我想创建一个php代码,当我点击按钮时,它可以立即更新所有行。已经尝试了一些方法,我从另一篇文章看,但仍然无法弄清楚。请帮帮我................
<table cellspacing="0" cellpadding="1" border=1px; action="" method="post">
<?php
$result=mysqli_query($conn,"SELECT * from subject ");
if($result->num_rows>0)
{
?>
<tr>
<th>Subject Code</th>
<th>Subject</th>
<th>Fees</th>
<th>Status</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($result))
{
if ($row["subject_status"]==="Available")
{$status="Unavailable";}
else $status="Available";
?>
<tr class="alttr1">
<td><?php echo $row["subject_code"]; ?></td>
<td><?php echo $row["subject_name"]; ?></td>
<td>RM<input type="number" name="price[]" value="<?php echo $row["subject_price"]; ?>" ></td>
<td><select name="status[]" ><option value="<?php echo $row["subject_status"]; ?>" ><?php echo $row["subject_status"]; ?></option><option value="<?php echo $status; ?>" ><?php echo $status; ?></option></select></td>
</tr>
<?php
}
?>
</table>
<input type="submit" name="updatebtn" value="Save Changes">
<?php
if (isset($_POST["updatebtn"]))
{
$price = $_POST["price"];
$status = $_POST["status"];
foreach($_POST["price"] as $price)
{
mysqli_query($conn,"UPDATE subject SET subject_price='$price' ");
}
foreach($_POST["status"] as $status)
{
mysqli_query($conn,"UPDATE subject SET subject_status='$status' ");
}
header("location: subjectmanage.php");
}
答案 0 :(得分:0)
将包含主题代码的隐藏字段添加到表单中。
<tr class="alttr1">
<td><?php echo $row["subject_code"]; ?><input type="hidden" name="code[]" value="<?php echo $row["subject_code"]; ?>"></td>
<td><?php echo $row["subject_name"]; ?></td>
<td>RM<input type="number" name="price[]" value="<?php echo $row["subject_price"]; ?>" ></td>
<td><select name="status[]" ><option value="<?php echo $row["subject_status"]; ?>" ><?php echo $row["subject_status"]; ?></option><option value="<?php echo $status; ?>" ><?php echo $status; ?></option></select></td>
</tr>
然后更新代码如下所示:
<?php
if (isset($_POST["updatebtn"]))
{
$codes = $_POST["code"];
$prices = $_POST["price"];
$statuses = $_POST["status"];
foreach($codes as $i => $code)
{
$code = mysql_real_escape_string($code);
$price = mysql_real_escape_string($prices[$i]);
$status = mysql_real_escape_string($statuses[$i]);
mysqli_query($conn,"UPDATE subject SET subject_price='$price', status = '$status' WHERE subject_code = '$code'");
}
}