我不是想屈尊俯就,只是想清楚一点。
所以!我有一个包含数组的会话。我有"id",它是使用来自URL的GET["id"]设置的(例如:php?id=1)。我还有"数量",自动为1。
当向会话中添加一些id号时,假设id=>1、id=>2、id=>3,当然还有所有三个数组的数量=>1。
我想更新"id"编号2的"qty"值,而不更改其他id编号(1和3)的"qty"值。我想为每个id做这件事。
Bellow是我的代码,运行时将其保存为cart.php。然后运行三次,第一次是localhost/cart.php=1,localhost/cart.php=2,localhost/cart.php=3.这样您就可以在会话中存储三个身份证号码。然后转到cart.hp.
index.php**代码
<div class="product">
<h3>Baketball</h3>
<a href="add-to-cart.php?id=1">Add to cart</a>
</div>
<div class="product">
<h3>Football</h3>
<a href="add-to-cart.php?id=2">Add to cart</a>
</div>
<div class="product">
<h3>Baseball</h3>
<a href="add-to-cart.php?id=3">Add to cart</a>
</div>
cart.php**代码
// set the array and store in session
$_SESSION['items'][]=array(
'id'=>"'".$_GET['id']."'",
'qty'=> 1
);
// Display each array value stored in the session
foreach ($_SESSION['items'] as $value) {
// Display the id
echo 'The ID is ' ."''''" . $value['id'] ."''''" ;
// Display the Qty
echo 'the QTY ' ."''''" . $value['qty'] ."''''" ;
echo "<br/>";
//Display and edit quantity in a form.
echo "<form method='post' action='id.php' name='edit'>
<p>Quantity: <input type="text" size="20" name='".$value['id']."' value='".$value['qty']."'/> </p><br>
<input class='save' name='update' value='update' type='submit'>
</form>";
}
// check if the update button is submited so that the qty value can be changed where the id number of the selected qty input box is edited
if (isset($_POST["update"])) {
$_SESSION['item'][$id]= $_POST["'".$value['id']."'"];
}
?>
在您发布的其余代码中,我没有看到$id
的设置位置。所以我会在这里再次定义它,只是为了确定。
将您的输入更改为:
echo "<p>ID: <input type='text' name='id' value='" . $value['id'] . "'></p>";
echo "<p>Quantity: <input type='text' size='20' name='qty' value='" . $value['qty'] . "'/></p>";
理想情况下,id字段应该是type="hidden",但至少您可以看到用于测试的数字。
然后你可以使用它来更新你的会话:
$id = $_POST['id'];
$qty = $_POST['qty'];
$_SESSION['item'][$id] = $qty;