购物车尝试数量问题(php)

时间:2015-04-21 12:24:36

标签: php mysql session shopping-cart

HELP: 我目前正在通过PHP会话购物车。 从产品中的链接添加到购物车位工作正常。我可以添加项目。 但问题是我的购物车上有一个数量文本框,允许我更改我想要在购物车中的项目数量。然后每当我输入0时,我希望从购物车中删除所述项目。 以下是我的代码:

<?php 
    /* MYSQL Connection */
    $servername = "example.com";
    $username = "a12341457";
    $password = "asdfj";
    $database = "a1234567";

$con = mysqli_connect($servername, $username, $password, $database);

if(!$con){
    die("Fail to connect to server ".mysqli_connect_error());
 }
?> 
<?php session_start();

if(isset($_GET['action']) && $_GET['action'] == "add")
{
    $id = intval($_GET['product_id']);

    if(isset($_SESSION['cart'][$id])){
        $_SESSION['cart'][$id]['quantity']++;
    } else {
        $sql2 = "SELECT * FROM ShopProducts WHERE product_id=($id)";
        $query2 = mysqli_query($con, $sql2);

        if(mysqli_num_rows($query2)!= 0){
            $row2 = mysqli_fetch_array($query2);
            $_SESSION['cart'][$row2['product_id']] = array("quantity" => 1, "price"=> $row2['product_price']);
        } else {
            $message = "This product ID is invalid";
        }
    }
   }//error_reporting(0);
 ?> 
<h2 class="message"><?php if(isset($message)){echo $message;}?></h2>

<?php
// QUANTITY COUNTER CONTROL:
    if(isset($_POST['submit'])){
        foreach($_POST as $key => $value){
            $key = explode("-", $key); 
            $key = end($key);  
            $key = explode("submit", $key); 
            $key = end($key);   

        if($_POST['quantity-'.$key] <= 0){
            unset($_SESSION['cart'][$key]);
        } else if($_POST['quantity-'.$key] >= 50){
            $_SESSION['cart'][$key]['quantity'] = 50;
        }else {
            $_SESSION['cart'][$key]['quantity'] = $value;
        }
    }
} 
?>

<h1>View Cart</h1>
<a href="index.php" title="go back to products">Go to Products</a>
<?php 
$sql = "SELECT * FROM ShopProducts WHERE product_id IN (";
    foreach($_SESSION['cart'] as $id => $value){
        $sql .= $id.",";
    }
    $sql = substr($sql, 0, -1). ") ORDER BY product_name ASC";
    $query = mysqli_query($con, $sql);
if(empty($query)){
        echo "<br />You need to add an item before you can view it here";
    }
?>

<form method="POST" action="cart.php">
<fieldset>
    <table>
        <tr>
        <th>NAME</th>
        <th>QUANTITY</th>
        <th>UNIT PRICE</th>
        <th>TOTAL AMOUNT</th>
    </tr>
 <?php 
   $sql = "SELECT * FROM ShopProducts WHERE product_id IN (";
   foreach($_SESSION['cart'] as $id =>     $value){
    $sql .= $id.",";
}
$sql = substr($sql, 0, -1). ") ORDER BY product_name ASC ";
$query = mysqli_query($con, $sql);
$totalcost = 0;
if(!empty($query)){
    while($row = mysqli_fetch_array($query)){
        $subtotal = $_SESSION['cart'][$row['product_id']]['quantity'] * $row['product_price'];
        $totalcost += $subtotal;

?>
<tr>
<td><?php echo $row['product_name']; ?> </td>
<td><input type="text" name="<?php echo $row['product_id']; ?>" size="5"     value="<?php echo $_SESSION['cart'][$row['product_id']]['quantity']; ?>" /></td>
<td><?php echo "&pound;".$row['product_price'];?></td>
<td><?php echo "&pound;".$_SESSION['cart'][$row['product_id']]['quantity']*$row['product_price'];?></td>
</tr>
<?php
}
}
?>
   <tr>
    <td></td>
    <td></td>
    <td>TOTAL: </td>
    <td><?php echo "&pound;".$totalcost;?></td>
</tr>
</table>
<input type="submit" name="submit" value="Update Cart" /></fieldset>
</form>
<p>To remove an item, set quantity to 0</p>

每当我更改数量文本框中的值时,我都会收到以下PHP / MySQL错误: 注意:未定义的索引:第46行的C:\ wamp \ www \ final_ \ cart.php中的数量为2

0 个答案:

没有答案