Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

490
Views
How to use the values of a dropdown list in an sql statement in php

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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:

  • Bouncing in and out of php using <?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.
  • Unless you are going to use a variable more than once, don't bother declaring it. This is why I write the query statement directly inside of mysqli_query() and place the $row[column] variable directly in the option tag.
  • Always check your query result to be true before calling any 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.
  • If an option's text and value are the same, omit the value attribute. The value will be the text value on submission.
  • It is good practice to free your results when you are done with them.
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!