I'm writing a php code to get the value from a dropdown list and to show it into a table. The drop down list is populated directly from my db. The code of populating the dropdown list is:
<select name = "Service1">
<option>Select</option>
<?php
$con=getdb();
$query1="SELECT DISTINCT Service FROM pay";
$result1=mysqli_query($con,$query1);en
while($rows1=mysqli_fetch_array($result1)){
$rowsData1=$rows1['Service'];
?>
<option value=""><?php echo $rowsData1 ?></option>
<?php
}
?>
</select>
<select name = "Terminale1">
<option>Select</option>
<?php
$query2="SELECT DISTINCT Terminal FROM pay";
$result2=mysqli_query($con,$query2);
while($rows2=mysqli_fetch_array($result2)){
$rowsData2=$rows2['Terminal'];
?>
<option value=""><?php echo $rowsData2 ?></option>
<?php
}
?>
</select>
And this works because it shows me the values in the dropdown list.
Now i have a Submit button that when I click on it it has to show me a table with the values that i have select in the query below:
<?php
if(isset($_POST['submit']))
{
$service2=$_POST['Service1'];
$Terminale2=$_POST['Terminale1'];
$query3="SELECT Date, Service, Status
FROM mytable
WHERE Service ='".$service2."' AND Terminal='".$Terminale2."'";
$result3=mysqli_query($con,$query3);
while($rows3=mysqli_fetch_array($result3)){
$dataime=$rows3['Date'];
$Service=$rows3['Service'];
$Status=$rows3['Status'];
?>
<tr>
<td><?php echo $Date ?></td>
<td><?php echo $Service ?></td>
<td><?php echo $Status ?></td>
</tr>
<?php
}
}
?>
When i select the values from the droplist it doesn't show me any record or error in my table. What am i doing wrong?
JustOnUnderMillions knows to submit answers as answers and not comments, but occasionally doesn't do so when the fix is minor. Unfortunately this causes a question to appear unresolved / abandoned.
I'll submit an answer for you to accept and beef it up with a few refinements.
$con=getdb();
if($result=mysqli_query($con,"SELECT DISTINCT Service FROM pay")){
echo "<select name=\"Service1\">";
echo "<option>Select</option>";
while($row=mysqli_fetch_assoc($result)){
echo "<option>{$row["Service"]}</option>";
}
mysqli_free_result($result);
echo "</select>";
}else{
echo "Syntax Error On Service Query";
}
if($result=mysqli_query($con,"SELECT DISTINCT Terminal FROM pay")){
echo "<select name=\"Terminale1\">";
echo "<option>Select</option>";
while($row=mysqli_fetch_assoc($result)){
echo "<option>{$row["Terminal"]}</option>";
}
mysqli_free_result($result);
echo "</select>";
}else{
echo "Syntax Error On Terminal Query";
}
Advice:
<?php and ?>, in my opinion, makes the code harder to read and the slightest typo can be harder to find. Though not required, I recommend staying "in" php unless you have an unusually large portion of code that is pure html.mysqli_query() and place the $row[column] variable directly in the option tag.mysqli_fetch_ functions. Because the result variable needs to be checked and then used again later, it is simplest to declare it and conditionally check it in one line.