简体   繁体   English

foreach 中的表单操作仅适用于 php 的第一个值

[英]form action inside foreach works only for the first value of php

I have created a form where every time, it would store a different id during foreach loop我创建了一个表单,每次它都会在 foreach 循环期间存储不同的 id

 <?php foreach ($res as $r) { ?> <form id="clearitem" action="clearitem.php" method="POST" enctype="multipart/form-data"> <input type="text" name="clearitem" value="<?php echo $r['cart_id'];?>"> <button type="Submit">&times;</button> </form> <?php } ?>

and the retrieval of the data works perfectly, it shows me each and every id one after another, but when I click the button (in this case to clear the item) it only works for the first value ie if I click the button for the second or the third value, it deletes the first one only, not the one corresponding to the button.并且数据的检索工作完美,它一个接一个地向我显示每个 id,但是当我单击按钮(在这种情况下是清除项目)时,它仅适用于第一个值,即如果我单击按钮第二个或第三个值,它只删除第一个,而不是与按钮对应的那个。

please do help, why this is happening and the appropriate solution, if anyone can provide!如果有人可以提供,请帮忙,为什么会发生这种情况以及适当的解决方案!

the clearitem.php clearitem.php

 if(isset($_POST['clearitem'])){ $clear=$_POST['clearitem']; $sql = "DELETE FROM cart WHERE id=".$clear; if ($conn->query($sql) === TRUE) { echo "Record deleted successfully of id".$clear; } else { echo "Error deleting record: " . $conn->error; } }

So, I was able to solve this issue, and the solution is the "id" so when you are using foreach, for every iteration, it is re-creating the form with the different value of the input, of course!所以,我能够解决这个问题,解决方案是“id”,所以当你使用 foreach 时,对于每次迭代,它当然是用不同的输入值重新创建表单!

But the problem is the id-field, even if the input value is different, but the id is same for every iteration and therefore, even if you click a different button ie the 2nd or 3rd, the POST request will consider the initial value as the actual value, ie the first iteration, just because the id was same!但问题是 id 字段,即使输入值不同,但每次迭代的 id 都是相同的,因此,即使您单击不同的按钮,即第二个或第三个,POST 请求也会将初始值视为实际值,即第一次迭代,只是因为 id 相同!

It's similar to the concept of first-come-first-serve.这类似于先到先得的概念。 Since the POST request getting the value of the first element correspondent to the id, therefore that will the final value of that specific id and no amount how many times you iterate, the value will not be updated if you use the same id.由于 POST 请求获取与 id 对应的第一个元素的值,因此这将是该特定 id 的最终值,并且无论您迭代多少次,如果您使用相同的 id,该值将不会更新。

The possible fix is to have a different id, with each different iteration and that was made possible with putting the foreach value in the id.可能的解决方法是在每次不同的迭代中使用不同的 id,这可以通过将 foreach 值放在 id 中来实现。

 <?php foreach ($res as $r) { ?> <form id="clearitem<?php echo $r['cart_id'];?>" action="clearitem.php" method="POST" enctype="multipart/form-data"> <input type="text" name="clearitem" value="<?php echo $r['cart_id'];?>"> <button type="Submit">&times;</button> </form> <?php } ?>

by this for every iteration, the value of the id will be different, like if cart_id are as:1, 2, 3 then the ids would be clearitem1, clearitem2, clearitem3.这样,对于每次迭代,id 的值都会不同,例如如果 cart_id 为:1、2、3,那么 id 将为 clearitem1、clearitem2、clearitem3。 By this the POST request will be retrieving different values since the ids are different!这样,POST 请求将检索不同的值,因为 id 不同!

The system should be able to correctly process the submitted data according to the data value in each form in such a "multi-form" environment.在这样的“多表单”环境下,系统应该能够根据in each form中的数据值正确处理提交的数据。

Hence the code below should work (tested):因此,下面的代码应该可以工作(经过测试):

<?php

$conn = new mysqli("localhost","dbuser","dbpassword","dbname");

$sql = "SELECT * FROM cart";
$stmt = $conn->prepare($sql); 
$stmt->execute();
$res = $stmt->get_result();

foreach ($res as $r) {
?>

<form id="clearitem" action="clearitem.php" method="POST" enctype="multipart/form-data">
<input type="text" name="clearitem" value="<?php echo $r['cart_id'];?>">
<button type="Submit">&times;</button>
</form>
<?php } ?>

The following is the clearitem.php code以下是clearitem.php代码

<?php

if (isset($_POST["clearitem"])) {

$conn = new mysqli("localhost","dbuser","dbpassword","dbname");
$sql = "delete FROM cart where cart_id=?" ;
$stmt = $conn->prepare($sql); 

$id=$_POST["clearitem"]; 
$stmt->bind_param("i", $id);
$stmt->execute();
}
?>

<script>
    alert("Done !");
    if ('referrer' in document) {
        window.location = document.referrer;
        /* OR */
        //location.replace(document.referrer);
    } else {
        window.history.back();
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM