这是我的第二个代码,但问题是我有3个查询。所以当我点击更新时,它只返回最后一个product_id,它总是返回product_id=3,但我想更新product_id=2
<form action="update_qty.php" method="POST">
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price">
<input type="hidden" value="<?=$getorder['product_id']?>" name="product">
<input type="text" value="<?=$getorder['qty']?>" name="qty" size="1" style="text-align:center">
<input type="submit" value="update" name="update">
<?php } ?>
</form>
您的问题是PHP是服务器端的,您需要一些客户端来读取文本框的值。您需要刷新页面以将文本字段值传递给服务器,这样服务器就可以将其写入锚点标记中的url。这就是表单提交所要做的,但由于它已经提交了值,锚标签将是毫无意义的
要在不刷新页面的情况下完成此操作,请使用Javascript。使用jQuery会很容易。您可以添加一个事件,在键入时将文本框中输入的内容写入锚标记href。
我会做一些类似的事情。每个产品一个表单。在您的情况下,当您提交表格时,数量值将始终是找到的las。
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<form action="update_qty.php" method="POST">
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price">
<input type="hidden" value="<?=$getorder['product_id']?>" name="product">
<input type="text" value="<?=$getorder['qty']?>" name="qty" size="1" style="text-align:center">
<input type="submit" value="update" name="update">
</form>
<?php } ?>
您可以添加更多类似的信息
<a href="update_qty.php?product_id=<?=$getorder['product_id']?>&type=text">update</a>
不能像那样获取所有值,因为在每次循环迭代中都会覆盖输入名称。
对于多个值,您可以尝试两种方式,如:
<?php
while($getorder = mysqli_fetch_array($order)){
$newArr[] = $getorder['price']."~".$getorder['product_id']."~". $ getorder['qty'];
} //while end
?>
<input type="hidden" name="allinputs" value="<?=$newArr?>">
循环外的输入字段。
在php中,用~分解数组值并获取所有值。
另一个解决方案是您的输入字段名称必须更改为:
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price_<?=$getorder['product_id']?>">
<?php } ?>
在每次迭代中更改字段名称。
在当前的场景中,您需要三个不同的按钮或使用AJAX请求的最佳解决方案。
<a href="update_qty.php?product_id=<?php echo $getorder['product_id']?>">update</a>
在update_qty.php
上,你可以像这样使用
<?php echo $_GET['product_id'];?>