我想在“Class 1”中显示学生表。用户可以更改信息并将其更新到数据库中。我想一次更新多行,但最终只更新了 1 行(最后一行)。请帮助我..当我单击更新时,我应该如何更改代码以便一次更新多行。谢谢。
这是代码:
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'test');
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
mysql_set_charset("utf8", $connection);
?>
<form action='update.php' method='post'>
<table border='1'>
<?php
$result = mysql_query("SELECT * FROM student WHERE class = 1 ");
echo "<tr>";
echo "<td colspan='3'>CLASS 1</td>";
echo "</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td><input type='hidden' name='id' value='".$row['id']."' /></td>";
echo "<td>name :<input type='text' name='name' value='".$row['name']."' /></td>";
echo "<td>Sex :<input type='text' name='sex' value='".$row['sex']."' /></td>";
echo "<td>Age :<input type='text' name='age' value='".$row['age']."' /></td>";
echo "</tr>";
}
echo "<input type='submit' name='update' value='UPDATE' />";
?>
<table>
</form>
<?php
if(isset($_POST['update'])){
$id = $_POST['id'];
$name= $_POST['name'];
$sex = $_POST['sex'];
$age = $_POST['age'];
mysql_query(" UPDATE student
SET name= '$name', sex='$sex', age='$age'
WHERE id = '$id' ");
header("location: update.php");
exit;
}
?>