将选定的产品尺寸传递到购物车

时间:2019-01-17 09:50:18

标签: php mysql shopping-cart

我在“产品详细信息”页面上具有带有尺寸选项的下拉菜单,但是将产品添加到购物车时,列尺寸为空。下面是我的代码。任何建议或帮助将不胜感激。

产品详细信息页面

if(isset($_GET['action']) && $_GET['action']=="add"){
$id=intval($_GET['id']);
$sizeoption=$_GET['productSize'];
if(isset($_SESSION['cart'][$id]) && isset($_SESSION['cart'][$id]['productSize']) && $_SESSION['cart'][$id]['productSize'] == $sizeoption){
    $_SESSION['cart'][$id]['quantity']++;
}else{
    $sql_p="SELECT * FROM products WHERE id={$id}";
    $query_p=mysqli_query($con,$sql_p);
    if(mysqli_num_rows($query_p)!=0){
        $row_p=mysqli_fetch_array($query_p);
        $_SESSION['cart'][$row_p['id']]=array("quantity" => 1, "price" => 
 $row_p['productPrice'], 'productSize' => $sizeoption);
        header('location:my-cart.php');
    }else{
        $message="Product ID is invalid";
       }
    } 
  } 


  **/Select size

 <select name="productSize" class="span8 tip" required>
 <?php $query=mysqli_query($con,"select size.id, size.sizeName, 
  products.productSize from size INNER JOIN products ON 
  size.id=products.productSize WHERE products.id='$pid'");
  while($row = mysqli_fetch_array($query)) {
  $size=$row['productSize'];
  $boom = explode(',', $size);
  foreach ($boom as $row){
  echo '<option value='.$row.'>'.$row.'</option>'; 
        }} ?>
  </select>

购物车表

  <?php
  $pdtid=array();
  $sql = "SELECT * FROM products WHERE id IN(";
        foreach($_SESSION['cart'] as $id => $value){
        $sql .=$id. ",";
        }
        $sql=substr($sql,0,-1) . ") ORDER BY id ASC";
        $query = mysqli_query($con,$sql);
        $totalprice=0;
        $totalqunty=0;
        if(!empty($query)){
        while($row = mysqli_fetch_array($query)){
            $quantity=$_SESSION['cart'][$row['id']]['quantity'];
            $subtotal= $_SESSION['cart'][$row['id']]['quantity']*$row['productPrice'];
            $totalprice += $subtotal;
            $_SESSION['qnty']=$totalqunty+=$quantity;

            array_push($pdtid,$row['id']);
      //print_r($_SESSION['pid'])=$pdtid;exit;
      ?>

<td class="cart-product-name-info"><a href="product-details.php?pid=<?php echo htmlentities($pd=$row['id']);?>" ><?php echo $_SESSION['cart'][$row['id']]['productSize'];?></a></td>

0 个答案:

没有答案