0

我已经对购物车进行了编码,并且通过刷新我的产品增加了一个我搜索了很多并且在stackoverflow中每个人都说在获得你的产品后重定向但尝试它对我不起作用请帮助我她的代码

///代码///

<?php
if(isset($_GET['prod']))
{
$current_id =  $_GET['prod']; 
}
mysql_connect('localhost','root','password'); 
mysql_select_db('shopper');
session_start(); 
if (isset($_GET['prod'])&&($_GET['action'])){
$product_id = $_GET['prod']; //the product id and action for the case from the URL 
$action = $_GET['action'];}
function productExists($product_id) {// for product to check from DB

    $sql = sprintf("SELECT * FROM products WHERE id = %d;", $product_id); 

    return mysql_num_rows(mysql_query($sql)) > 0;
}
if($product_id && !productExists($product_id)) {
    die("Error. Product Doesn't Exist");
}

switch($action) { //decide what to do  to add or to remove or to delete

    case "ADD":
        $_SESSION['cart'][$product_id]++;
        //header("location: cart.php");     //add one to the quantity of the product with id $product_id 
    break;

    case "minus":
        $_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id 
        if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]);
//header("location: cart.php");     //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items. 
    break;

    case "delete":
        unset($_SESSION['cart']); 
        //header("location: cart.php");//unset the whole cart, i.e. empty the cart. 
    break;

}
if($_SESSION['cart']) { to display to my website the quantity the name the picture the total

    foreach($_SESSION['cart'] as $product_id => $quantity) { 
$result = mysql_query ("SELECT * FROM `products` WHERE `id` = $product_id ");
if (mysql_num_rows($result) == 0 )
{ echo "there is nothing to display ";  
}

else{ 
while ($get_row = mysql_fetch_assoc($result)){
$sub = $get_row['price'] * $quantity ;
$tbl[] = '<tr>'
                      . '<td>'.'<img src="'.$get_row['image_path'].'" alt="" />'. '</td>'
                      . '<td>' . $get_row['name']. '</td>'

                      . '<td>'
                      . '<a href="cart.php?prod='.  $current_id .'&action='.  "ADD" .'">[+] </a> '
                      . '<a href="cart.php?prod='.  $current_id .'&action='.  "minus" .'">[-] </a>'
                      . '<a href="cart.php?prod='.  $current_id .'&action='.  "delete" .'">[Empty the cart] </a>'                                                
                      . '</td>'
                      . '<td>' . $quantity . '</td>'
                      . '<td>' .$get_row['price'] . '</td>'
                      . '<td>' .$sub . '</td>'

                      . '</tr>'
                    ;
                }
                $tbl[] = '</tbody>';
                 $total = $total +$sub ;
            }
        }
    }
?>

此代码正在显示产品并执行所有操作并给我总计但有以下错误

///错误///

Notice: Undefined offset: 7 in C:\wamp\www\shopper_new\cart.php on line 31 (7 is my product id )


Notice: Undefined variable: total in C:\wamp\www\shopper_new\cart.php on line 78


Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\shopper_new\cart.php on line 54

但问题是当我刷新我的页面产品更新时 //

4

0 回答 0