我正在使用隐藏字段添加数据。最初它可以工作并显示我在数据库中需要的所有信息。但是,当我再次使用它时,它只捕获与之前已捕获的先前相同的名称。
例如,有一个苹果的图像,当我添加时,它应该显示名称为苹果。然后,当我点击梨的图像时,显然它应该在我的数据库中显示'pear'作为名称。但它不是'梨',而是显示为苹果。有谁知道为什么?
<form action="addOrder.php" method="post">
<td class="timgG"><h4><img src="apple.jpg" style="vertical-align: text-bottom;" title="Apple"/> Apple <br>
<input type="hidden" name="op" value="add">
<input type="hidden" name="name" value="Apple">
<input type="hidden" name="price" value="0.50">
<input type="submit" value="Add to Cart">
</h4></td>
<td class="timgG"><h4><img src="pear.jpg" style="vertical-align: text-bottom;" title="Pear"/> Pear <br>
<input type="hidden" name="op" value="add">
<input type="hidden" name="name" value="Pear">
<input type="hidden" name="price" value="0.50">
<input type="submit" value="Add to Cart">
</h4></td>
</form>
<?php
if (isset($_SESSION['user_id'])) {
//$order_id = $_POST['order_id'];
$name = $_POST['name'];
//$quantity = $_POST['quantity'];
$price = $_POST['price'];
$query = "INSERT INTO order_details (name,price) VALUES ('" . $name . "','" . $price . "')";
$status = mysqli_query($link, $query) or die(mysqli_error($link));
if ($status) {
$msg = "Item has been added.<br />";
$msg .= "<a href='product.php'>Back</a></p>";
}
} else {
$msg = "There was an error processing the form.Please try again <a href=girls.php>Back";
}
?>
答案 0 :(得分:1)
您的HTML
输入具有相同的name
:
<input type="hidden" name="name" value="Apple">
<input type="hidden" name="name" value="Pear">
由于Apple
是第一个,这是PHP
处理的第一个。{
修复方法是使用两个不同的表单和唯一的提交按钮名称,或者为隐藏的输入设置唯一的名称。
答案 1 :(得分:0)
您对同一表单中的不同产品使用相同的name
属性,因此您将覆盖表单字段。
您可以使用数组包含多个具有相同名称的项目:
<input type="hidden" name="name[]" value="Apple">
// etc.
然后在php中,$_POST['name']
也将是一个数组。
你真的应该转向准备好的陈述。
答案 2 :(得分:0)
为每件商品使用个人<form>
。当前设置(如果您以不同方式设置其他参数,您会注意到)始终使用表单中隐藏输入的第一个定义。
尝试使用
<form action="addOrder.php" method="post">
<td class="timgG"><h4><img src="apple.jpg" style="vertical-align: text-bottom;" title="Apple"/> Apple <br>
<input type="hidden" name="op" value="add">
<input type="hidden" name="name" value="Apple">
<input type="hidden" name="price" value="0.50">
<input type="submit" value="Add to Cart">
</h4></td>
</form>
<form action="addOrder.php" method="post">
<td class="timgG"><h4><img src="pear.jpg" style="vertical-align: text-bottom;" title="Pear"/> Pear <br>
<input type="hidden" name="op" value="add">
<input type="hidden" name="name" value="Pear">
<input type="hidden" name="price" value="0.50">
<input type="submit" value="Add to Cart">
</h4></td>
答案 3 :(得分:0)
使用两种不同的形式
<form action="addOrder.php" method="post">
<td class="timgG"><h4><img src="apple.jpg" style="vertical-align: text-bottom;" title="Apple"/> Apple <br>
<input type="hidden" name="op" value="add">
<input type="hidden" name="name" value="Apple">
<input type="hidden" name="price" value="0.50">
<input type="submit" value="Add to Cart">
</h4></td>
</form>
<form action="addOrder.php" method="post">
<td class="timgG"><h4><img src="pear.jpg" style="vertical-align: text-bottom;" title="Pear"/> Pear <br>
<input type="hidden" name="op" value="add">
<input type="hidden" name="name" value="Pear">
<input type="hidden" name="price" value="0.50">
<input type="submit" value="Add to Cart">
</h4></td>
</form>
也有一些人提到在数据库中插入东西时使用mysqli_real_escape_string()