我想使用select选项来更新表。 这些是代码,当我尝试它时,没有数据显示到表中。请帮我看看这些代码。如何使用php和mysql中的select选项更新表
这些代码用于选择选项。
<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`");
\t \t while($row=mysqli_fetch_array($query)){
\t \t \t ?>
<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">
而这些代码是用于显示数据,以表。
<?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>
我想要的是当我从选择选项的表格会自动更新选择。
没有ajax和onchange事件,你不能根据选择的变化来做到这一点,你必须提交一个表单POST到php – clearshot66
您的代码容易受到[** SQL注入**](https://en.wikipedia.org/wiki/SQL_injection)攻击。您应该通过[** mysqli **](https://secure.php.net/manual/en/mysqli.prepare.php)或[** PDO **](https ://secure.php.net/manual/en/pdo.prepared-statements.php)驱动程序。 [**这篇文章**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)有一些很好的例子。 –