我制作了一个简单的购物车,我的问题是当我想要更新单个产品的数量时。我使用post方法来做到这一点。当我在正确的项目上添加数量并单击更新时,它会成功更新,但任何其他产品的数量也会更新。请帮忙。
<?php
if(isset($_POST['Update']))
{
if(isset($_POST['prod_id']))
{
$myid = $_POST['prod_id'];
if (in_array($myid,$_SESSION['cart']))
{
$key = array_search($myid, $_SESSION['cart']);
$value_qty = "qtyval".$key;
$_SESSION[$key] = $_POST[$value_qty];
$message=$_SESSION[$key];
echo '<script>alert("'.$message.'")</script>';
}
}
}
?>
<?php
foreach ($_SESSION['cart'] as $key_value => $listitem)
{
$sql="SELECT * FROM products where id = '$listitem'";
$result_set = $database->query($sql);
while ($row=$database->fetch_array($result_set)){
?>
<div class="basket_block" id="basket_block">
<div class="item_block_remove" id="item_block_remove"><a href="basket.php?remove=<?php echo $key_value; ?>">Remove</a></div>
<div class="item_block" id="item_block">Name: <?php echo $row['Title_of_Message']; ?><br />
Description <?php echo $row['Description']; ?>
</div>
<div class="item_qty" id="item_qty"><form action="" method="POST" enctype="multipart/form-data" name="frmqty">
<input type="text" name="<?php echo (string)'qtyval'.$key_value; ?>" size="3" style="border-radius: 15px;text-align: center;" value="<?php
if(isset($_POST['Update']))
{
echo $_SESSION[$key];
}
else
{
echo $_SESSION[$key] = 1;
}
?>" />
<input type="hidden" name="prod_id" id="prod_id" value="<?php echo $row['id']; ?>" />
<input type="submit" value="Update" name="Update" style="border:none;cursor:pointer;padding-top:10px; background-color:transparent;" />
</form></div>