Php 我只想知道如何改变代码中的数量?

Php 我只想知道如何改变代码中的数量?,php,mysqli,shopping-cart,Php,Mysqli,Shopping Cart,我是PHP新手,我正在为我的网站制作一个购物车。我想增加的可能性,以改变在同一页的项目数量。并在单击时显示项目 <?php session_start(); require 'connect.php'; require 'item.php'; if(isset($_GET['itemid'])){ $query = 'select * from items where itemid='.$_GET['itemid']; $result = mysqli_query($mysqli,$qu

我是PHP新手,我正在为我的网站制作一个购物车。我想增加的可能性,以改变在同一页的项目数量。并在单击时显示项目

<?php
session_start();
require 'connect.php';
require 'item.php';

if(isset($_GET['itemid'])){
$query = 'select * from items where itemid='.$_GET['itemid'];
$result = mysqli_query($mysqli,$query);
$items = mysqli_fetch_object($result);
$item = new Item();
$item->id = $items->itemid;
$item->name = $items->itemname;
$item->price = $items->price;
$item->quantity = 1;
// Check item is existing in cart
$index = -1;
$cart = unserialize(serialize($_SESSION['cart']));
for($i=0; $i<count($cart);$i++)
    if($cart[$i]->id==$_GET['itemid'])
    {
        $index = $i;
        break;
    }
if($index==-1)
    $_SESSION['cart'][]= $item;
else{
    $cart[$index]->quantity++;
    $_SESSION['cart'] = $cart;
}

}
//Delete item in cart
if(isset($_GET['index'])){
$cart = unserialize(serialize($_SESSION['cart']));
unset($cart[$_GET['index']]);
$cart = array_values($cart);
$_SESSION['cart'] = $cart;
}
?>
<table cellpadding="2" cellspacing="2" border="1" >
<tr>
<th>option</th>
<th>Id</th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Sub Total</th>
</tr>
<?php 
$cart = unserialize(serialize($_SESSION['cart']));
$s = 0;
for($i=0; $i<count($cart);$i++){
$s +=  $cart[$i]->price* $cart[$i]->quantity;
?>

<tr>
<td><a href="cart.php?index=<?php echo $index; ?>" onclick="return confirm('Are you sure?')">Delete</a></td>
<td><?php echo $cart[$i]->id; ?></td>
<td><?php echo $cart[$i]->name; ?></td>
<td><?php echo $cart[$i]->price; ?></td>
<td><?php echo $cart[$i]->quantity; ?></td>
<td><?php echo $cart[$i]->price* $cart[$i]->quantity; ?></td>
</tr>
<?php
$index++; 
}
?>
<tr>
<td colspan="5" align="right">Sum</td>
<td align="left" ><?php echo $s; ?></td>
</tr>
</table>
<br>
<a href="newfile.php">Continue Shopping</a>

选项
身份证件
名称
价格
量
小计
总和


一个简单而快速的解决方法是在表单中为“quantity”添加一个字段,并在php中传递一个$\u GET变量

$item->quantity = $_GET['quantity'];

你是在制作真正的购物车还是在学习php?因为这样您的代码就可以打开SQL注入$_直接将GET['something']传递到SQL查询中在PHP世界中是不允许的……请看下面这句话:“快来帮忙”-我们不在您的工资单上。@Fred ii-想要快一点,但半个小时都没有回来检查。@Dagon他们也一定是仙人掌种植者。@Dagon只是我不是说快一点:Dbtw,不要被否决票气馁。人们总是这样做,没有任何解释。你的答案比投票人给出的答案更有用。^我同意@castis。另外,这不是我的反对票。就我个人而言,我总是会对否决票做出解释/评论。感谢你的关注和友好的建议。