PHP MYSQL购物车

时间:2016-01-30 09:36:21

标签: php mysql

我尝试使用php ang mysql在谷歌搜索购物车中的代码。当我点击“添加到购物车”按钮时,我的cart.php中没有显示任何内容。请帮帮我。

这是我的product.php

<a class="btn btn-info btn-fill pull-right" href="index.php?page=inso94&action=add&prod_id=<?php echo $row['0'];?>">ADD TO CART</a>

这是我的cart.php

<?php
$prod_id = $_GET["prod_id"];     //the product id from the URL 
$action = $_GET["action"]; //the action from the URL 

switch($action) {   //decide what to do 
    case "add":
        $_SESSION['cart'][$prod_id]++; //add one to the quantity of the product with id $product_id 
        break;
    case "remove":
        $_SESSION['cart'][$prod_id]--; //remove one from the quantity of the product with id $product_id 
        if($_SESSION['cart'][$prod_id] == 0) unset($_SESSION['cart'][$prod_id]); //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 "empty":
        unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. 
        break;
}

if($_SESSION['cart']) {
    foreach($_SESSION['cart'] as $prod_id => $prod_quantity) { 
        $sql = mysql_query("SELECT * FROM product WHERE id = '$prod_id'"); 
        if(isset($res)) {
            while($result = mysql_fetch_assoc($sql)) {  
                $line_cost = $prod_price * $prod_quantity; 
                $total = $total + $line_cost;
                ?>
                <tr>
                    <td><?php echo $result["prod_name"]; ?></td>
                    <td><?php echo $result["prod_code"]; ?></td>
                    <td><?php echo $result["prod_category"]; ?></td>
                    <td><?php echo $result["prod_price"]; ?></td>
                    <td><?php echo $result["prod_quantity"]; ?></td>
                    <td><?php echo "<a href='$_SERVER[PHP_SELF]$action=remove&prod_id={$result['prod_id']}' class='btn btn-warning btn-fill btn-sm pull-right'>Remove</a>"; ?></td>
                    <td><?php echo $line_cost; ?></td>
                </tr>
                <?php 
            }
        }
    }
} else {
    echo "You have no items in your shopping cart."; 
}
?>

2 个答案:

答案 0 :(得分:1)

您要在产品ID上加1,但尚未初始化。 首先,将以下语句添加到代码顶部以启动会话:

session_start();

现在检查$ _SESSION ['cart']是否存在。如果没有,请创建它。

if(!isset($_SESSION['cart'])
$_SESSION['cart'] = array();

要添加时,在switch语句内,检查购物车中是否已存在该产品,然后向其中添加一个,否则用1初始化。

if(isset($_SESSION['cart'][$prod_id]))
$_SESSION['cart'][$prod_id]++;
else
$_SESSION['cart'][$prod_id] = 1;

您还没有初始化total。 您的代码中的某些逻辑有些错误。 while循环没有意义,因为每个id仅应在乘积中。 while循环应为if语句。

答案 1 :(得分:0)

您的查询和结果设置看起来都错了。

$sql = mysql_query("SELECT * FROM product WHERE id = '$prod_id'"); 
        if(isset($res)) {
            while($result = mysql_fetch_assoc($sql)) {  
                $line_cost = $prod_price * $prod_quantity; 
                $total = $total + $line_cost;
                ?>
                <tr>
                    <td><?php echo $result["prod_name"]; ?></td>
                    <td><?php echo $result["prod_code"]; ?></td>
                    <td><?php echo $result["prod_category"]; ?></td>
                    <td><?php echo $result["prod_price"]; ?></td>
                    <td><?php echo $result["prod_quantity"]; ?></td>
                    <td><?php echo "<a href='$_SERVER[PHP_SELF]$action=remove&prod_id={$result['prod_id']}' class='btn btn-warning btn-fill btn-sm pull-right'>Remove</a>"; ?></td>
                    <td><?php echo $line_cost; ?></td>
                </tr>
                <?php 
            }
        }
    }
} else {

我认为这就是你想要的?

$result = mysql_query("SELECT * FROM product WHERE id = '$prod_id'"); 
        if(isset($result)) {
            while($row = mysql_fetch_assoc($result)) {  
                $prod_price = $row['prod_price']
                $line_cost = $prod_price * $prod_quantity; 
                $total = $total + $line_cost;
                ?>
                <tr>
                    <td><?php echo $row ["prod_name"]; ?></td>
                    <td><?php echo $row ["prod_code"]; ?></td>
                    <td><?php echo $row ["prod_category"]; ?></td>
                    <td><?php echo $row ["prod_price"]; ?></td>
                    <td><?php echo $row ["prod_quantity"]; ?></td>
                    <td><?php echo "<a href='$_SERVER[PHP_SELF]$action=remove&prod_id={$result['prod_id']}' class='btn btn-warning btn-fill btn-sm pull-right'>Remove</a>"; ?></td>
                    <td><?php echo $line_cost; ?></td>
                </tr>
                <?php 
            }
        }
    }
} else {