简体   繁体   中英

How to update the table using the select option in php and mysql

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM