<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>
答案 0 :(得分:1)
两个表单字段都有相同的名称($ row [“HORSE_ID”]),这会导致问题!
答案 1 :(得分:1)
好吧,您在查询中将name
和height
都设置为$_POST[$horse_id]
:
$query = "UPDATE horse set horse_name = ".$_POST[$horse_id]."horse_height = ".$_POST[$horse_id]." WHERE horse_id = '".$horse_id."'";
你甚至给输入元素命名相同:
name="<?php echo $row["HORSE_ID"]; ?>"
输入元素的名称成为PHP端的$_POST
数组的键。因此,请为输入字段指定正确的名称,例如:
<input type="text" size="24"
name="horse_name[<?php echo $row["HORSE_ID"]; ?>]"
value="<?php echo $row["HORSE_NAME"]; ?>">
<input type="text" size="5"
name="horse_height[<?php echo $row["HORSE_ID"]; ?>]"
value="<?php echo $row["HORSE_HEIGHT"]; ?>">
并将您的查询更改为
"UPDATE horse set horse_name = ".mysql_real_escape($_POST['horse_name'][$horse_id])
."horse_height = ".mysql_real_escape($_POST['horse_name'][$horse_id])
." WHERE horse_id = '".$horse_id."'"
答案 2 :(得分:1)
<input type="text" size="24"
name="HorseName"
id="name-<?=row["HORSE_ID"] ?>"
value="<?php echo $row["HORSE_NAME"]; ?>"></td>
<td align="center">
<input type="text" size="5"
name="HorseHeight"
id="height-<?=row["HORSE_ID"] ?>"
value="<?php echo $row["HORSE_HEIGHT"]; ?>"></td>
<td align="center">
这样,你仍然可以使用id来设置特定马的复选框
更新:
继续为每匹马做这样的事情:
<input type="text" size="24" name="HorseName" value="<?= $row["HORSE_NAME"] ?>" />
<input type="text" size="5" name="HorseHeight"value="<?= $row["HORSE_HEIGHT"]; ?>" />
<input type="hidden" name="HORSE_ID" value="<?= $row["HORSE_ID"] ?>" />
然后你的php和查询看起来像这样:
$HORSE_NAME = $_POST['HORSE_NAME'];
$HORSE_HEIGHT = $_POST['HORSE_HEIGHT'];
$HORSE_ID = $_POST['HORSE_ID'];
"UPDATE table_name SET HORSE_NAME = $HORSE_NAME, HORSE_HEIGHT=$HORSE_HEIGHT WHERE HORSE_ID = $HORSE_ID"