仅返回查询的最后一个值

时间:2015-02-24 17:50:38

标签: php html

这是我的第二个代码,但问题是我有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>

5 个答案:

答案 0 :(得分:0)

您可以添加更多信息,例如

<a href="update_qty.php?product_id=<?=$getorder['product_id']?>&type=text">update</a>

答案 1 :(得分:0)

你的问题是PHP是服务器端,你需要客户端的东西来读取文本框的值。您需要页面刷新才能将文本字段值传递给服务器,以便将其写入锚标记中的URL。表单提交的内容是什么,但由于它已经提交了值,因此锚标记将毫无意义

要在没有页面刷新的情况下使用Javascript。使用jQuery很容易。您可以添加一个事件,该事件会在键入时将锚标记href写入文本框中输入的内容。

答案 2 :(得分:0)

<a href="update_qty.php?product_id=<?php echo $getorder['product_id']?>">update</a>

update_qty.php上你可以像这样使用

<?php echo $_GET['product_id'];?>

答案 3 :(得分:0)

我会做更像这样的事情。 每个产品一个表单。在您提交表单的情况下,qty值将始终为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 } ?>

答案 4 :(得分:0)

您无法获得所有值,因为输入名称会在每次循环迭代中覆盖。

对于多个值,您可以尝试两种方式:

<?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请求的最佳解决方案。

相关问题