2015-02-24 41 views
-1

這是我的第二個代碼,但問題是我有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> 
+0

不要以爲你可以這樣做。 GET值可以使用URL或表單標籤使用method =「GET」傳遞(在這種情況下,POST可能更好)。如果你不想要一個明確的表單/提交按鈕,你最好使用AJAX/jQuery。 – Maximus2012 2015-02-24 17:53:23

回答

0

你可以這樣

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

它與我的代碼相同 – 2015-02-24 17:56:29

+0

不,我添加了'&type = text' – smowtion 2015-02-24 17:57:58

0

添加更多的信息,你的問題是PHP是服務器端,你需要客戶端來讀取文本框的值。您需要刷新頁面才能將文本字段值傳遞給服務器,以便將其寫入錨標記中的url。這是表單提交會做什麼,但它會提交的價值已經錨定標記將是毫無意義的

要做到這一點,沒有頁面刷新使用Javascript。用jQuery很容易做到。您可以添加一個事件,該事件會在輸入文本框中輸入錨定標記href時輸入的內容。

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

update_qty.php u可以使用這樣

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

我會做更多的事情是這樣的。 每種產品的一種形式。在你的情況下,當你提交表格時,數量值將始終是找到的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 } ?> 
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請求的最佳解決方案。