2016-01-30 116 views
0

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

这是我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."; 
} 
?> 
+0

你知道session应该用'session_start()'函数启动吗? –

+0

是的。已经在顶部 –

回答

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 { 
+0

ty声明您的评论,我解决它。但仍然没有显示出来。 –