php从多个复选框更新数据
我有一个名为 horse 的表,我想创建一个表单来编辑 horse_name 和 horse_height,但数据无法更新,事实证明更新查询存在问题,当我打印查询以显示值时, horse_name 值等于 horse_height
<body bgcolor="#009933">
<?php
include("connection.php");
$conn = oci_connect($UName,$PWord,$DB);
if(empty($_POST["check"]))
{
$query="SELECT * FROM horse ORDER BY horse_name";
$stmt = oci_parse($conn, $query);
oci_execute($stmt);
?>
<center> <font face="Arial" size="3"><b>Horse Details</b></font>
</center>
<form method="post" action="multiplehorsemodify.php">
<table border="1" align="center">
<tr>
<th>Horse ID</th>
<th>Name</th>
<th>Height</th>
<th>Edit?</th>
</tr>
<?php
while($row = oci_fetch_array ($stmt))
{
?>
<tr>
<td><?php echo $row["HORSE_ID"]; ?></td>
<td align="center">
<input type="text" size="24"
name="<?php echo $row["HORSE_ID"]; ?>"
value="<?php echo $row["HORSE_NAME"]; ?>"></td>
<td align="center">
<input type="text" size="5"
name="<?php echo $row["HORSE_ID"]; ?>"
value="<?php echo $row["HORSE_HEIGHT"]; ?>"></td>
<td align="center">
<input type="checkbox" name="check[]" value="<?php echo $row["HORSE_ID"]; ?>">
</td>
</tr>
<?php
}
?>
</table>
<center>
<input type="submit"
value="Update Selected Titles" />
</center>
</form>
<?php
oci_free_statement($stmt);
}
else
{
foreach($_POST["check"] as $horse_id)
{
echo $horse_id;
$query = "UPDATE horse set horse_name = ".$_POST[$horse_id]."horse_height = ".$_POST[$horse_id]." WHERE horse_id = '".$horse_id."'";
echo $query;
$stmt = oci_parse($conn,$query);
oci_execute($stmt);
header("location: multiplehorsemodify.php");
}
}
oci_close($conn);
?>
</body>
i have a table called horse, i would like to create a form to edit the horse_name and horse_height, but the data cannot be updated, it turns out there is a problem with the update query, when i print the query to show the values, the horse_name value is equal to horse_height
<body bgcolor="#009933"> <?php include("connection.php"); $conn = oci_connect($UName,$PWord,$DB); if(empty($_POST["check"])) { $query="SELECT * FROM horse ORDER BY horse_name"; $stmt = oci_parse($conn, $query); oci_execute($stmt); ?> <center> <font face="Arial" size="3"><b>Horse Details</b></font> </center> <form method="post" action="multiplehorsemodify.php"> <table border="1" align="center"> <tr> <th>Horse ID</th> <th>Name</th> <th>Height</th> <th>Edit?</th> </tr> <?php while($row = oci_fetch_array ($stmt)) { ?> <tr> <td><?php echo $row["HORSE_ID"]; ?></td> <td align="center"> <input type="text" size="24" name="<?php echo $row["HORSE_ID"]; ?>" value="<?php echo $row["HORSE_NAME"]; ?>"></td> <td align="center"> <input type="text" size="5" name="<?php echo $row["HORSE_ID"]; ?>" value="<?php echo $row["HORSE_HEIGHT"]; ?>"></td> <td align="center"> <input type="checkbox" name="check[]" value="<?php echo $row["HORSE_ID"]; ?>"> </td> </tr> <?php } ?> </table> <center> <input type="submit" value="Update Selected Titles" /> </center> </form> <?php oci_free_statement($stmt); } else { foreach($_POST["check"] as $horse_id) { echo $horse_id; $query = "UPDATE horse set horse_name = ".$_POST[$horse_id]."horse_height = ".$_POST[$horse_id]." WHERE horse_id = '".$horse_id."'"; echo $query; $stmt = oci_parse($conn,$query); oci_execute($stmt); header("location: multiplehorsemodify.php"); } } oci_close($conn); ?> </body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,您在查询中将
name
和height
都设置为$_POST[$horse_id]
:甚至还为输入元素指定了相同的名称:
输入元素的名称成为 PHP 端
$_POST
数组的键。因此,为输入字段指定正确的名称,例如:并将查询更改为
Well, you set
name
andheight
both to$_POST[$horse_id]
in your query:And you even give the input elements the same name:
The name of an input element becomes the key of the
$_POST
array on the PHP side. So give the input fields the right names, e.g.:and change your query to
您的两个表单字段具有相同的名称($row["HORSE_ID"]),这会导致问题!
Both of your form fields have the same name ($row["HORSE_ID"]) which will cause problems!
这样,您仍然可以使用 id 使特定马的复选框唯一
更新:
继续对每匹马执行此操作:
然后您的 php 和查询可以如下所示:
This way, you can still use the id to make the checkboxes for the particular horse unique
UPDATE:
Go ahead and do it like this for each horse:
Then your php and query can look like this: