1

I want to use the select option to update the table. these are the code and when I try it, no data have been display to table..Help me pls....

these code are for select option.

<form method="POST">
            <text class="sort"><span>Course:</span></text> 
            
           <select class="sel" name="select_course" id="select_course">
                 <?php
                $query=mysqli_query($conn,"select DISTINCT Course from `tbl_student`");
            while($row=mysqli_fetch_array($query)){
        ?>
                <option><?php echo $row['Course'];?></option>
                <?php
                  }
               ?>
           </select>
            <input class="btnsearch" type="button" name="submit" value="Search">  
            <input class="btn" type="button" name="btn_add" value="Add Student">  
            <input class="btn_import" type="button" name="import" value="Import File">  
                

And these code are for displaying data to table.

 <?php
                          echo '<table>
                        <tr>
                        <td>name</td>
                        <td>Id number</td>
                        <td>Course</td>
                        <td>School Year</td>
                        <td>Semester</td>
                        </tr>';
                     if(isset($_POST['submit']))
                     {
                         $result2 = mysqli_query($conn, "Select name,id_number,Course,school_year,semester from tbl_student where Course='".$_POST['select_course']."'");
                         
                         while($row=mysqli_fetch_row($result2)){
                             echo '<tr>
                         <td>'.$row["name"].'</td>
                         <td>'.$row["id_number"].'</td>
                         <td>'.$row["Course"].'</td>
                         <td>'.$row["school_year"].'</td>
                         <td>'.$row["semester"].'</td>
                         </tr>';
                         }
                          echo '</table>';
                     }   
                     ?>
               
            </form>

All i want is when I choose from the select option the table will be updated automatically.

Ares_SVX
  • 43
  • 6
  • Without ajax and an onchange event, you can't do it based on select change, you'd have to submit a form for it POST to php – clearshot66 Aug 30 '17 at 14:33
  • Your code is vulnerable to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use prepared statements with bound parameters, via either the [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) drivers. [**This post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has some good examples. – Alex Howansky Aug 30 '17 at 18:25

2 Answers2

0

The best way to do this is ajax but js is not tagged here, so you can do it with html and php like:

<form action="updateData.php">
  <select name="select_course" onchange="this.form.submit()">

onchange="this.form.submit()" will submit the form when selection changed to updateData.php, put the code to update the data in table here.

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
0
while($row=mysqli_fetch_row($result2)){ 

here is error in your code . you are using mysqli_fetch_row and in blow line you are using $row["name"] . just use mysqli_fetch_assoc then use $row["name"] . if you will use mysqli_fetch_row then you have to put index of data in database

Laeeq Khan Niazi
  • 568
  • 4
  • 11