如何使购物车不重复物品?

How to make cart not duplicate items?

我需要知道如何不使物品重复并将数量从 showitem.php 添加到数量
但是我在 sql 中更新了 table 但 table 没有任何反应我不知道为什么?

这是addtocart.php

<?php
session_start();
function addtocart($id,$qty){
 if (isset($id)){
    //connect to database
    $mysqli = mysqli_connect("localhost", "root", "", "e-com");
    //create safe values for use
    $safe_sel_item_id = mysqli_real_escape_string($mysqli,
            $id);
            $safe_sel_item_qty = mysqli_real_escape_string($mysqli,$qty);
            //validate item and get title and price
            $get_iteminfo_sql = "SELECT itemname FROM items WHERE itemid = '".$safe_sel_item_id."'";
            $get_iteminfo_res = mysqli_query($mysqli, $get_iteminfo_sql)
            or die(mysqli_error($mysqli));

            if (mysqli_num_rows($get_iteminfo_res) < 1) {

                //free result
                mysqli_free_result($get_iteminfo_res);

                //close connection to MySQL
                mysqli_close($mysqli);

                //invalid id, send away
                header("Location: seestore.php");
                exit;
            } else { 
                //get info
                while ($item_info = mysqli_fetch_array($get_iteminfo_res)) {
                    $item_title =  stripslashes($item_info['itemname']);
                }

                //free result
                mysqli_free_result($get_iteminfo_res);
                $sql = "select sel_item_qty from shooppertrack where sel_item_id = '" .
                        $safe_sel_item_id . "'";

                $res = mysqli_query($mysqli, $sql);
                while ($res_info = mysqli_fetch_array($get_iteminfo_res)){
                    $update_qty = $res['sel_item_qty'];
                }
                // does quantity exist?
                if (mysqli_num_rows($res) > 0 ) {
                    // get sel_item_qty, add 1, run update query
                "UPDATE shooppertrack SET sel_item_qty = sel_item_qty + $update_qty  
                WHERE sel_item_id = '" .$safe_sel_item_id . "'" ;
                } else {
                //add info to cart table
                $addtocart_sql = "INSERT INTO shooppertrack
                (session_id, sel_item_id, sel_item_qty,
                        date_added)
    VALUES ('".$_COOKIE['PHPSESSID']."',
    '".$safe_sel_item_id."',
    '".$safe_sel_item_qty."',
    now())";
    $addtocart_res = mysqli_query($mysqli, $addtocart_sql)
    or die(mysqli_error($mysqli));
                }                   
    //close connection to MySQL
    mysqli_close($mysqli);
    //redirect to showcart page
    header("Location: showcart.php");
    exit;
            }
} else {
    //send them somewhere else
    header("Location: seestore.php");
    exit;
}
}
addtocart($_POST['sel_item_id'],$_POST['sel_item_qty']);
?>

这是showitem.php

<?php
  function show($x){
      //connect to database 
  $mysqli = mysqli_connect("localhost", "root", "", "e-com"); 
  $display_block = "<h1>My Store - Item Detail</h1>";

  //create safe values for use 
  $safe_item_id = mysqli_real_escape_string($mysqli, $x); 

  //validate item 
  $get_item_sql = "SELECT  c.cat_id , c.cat_name, si.itemid, si.itemname, 
  si.price, si.descripition, si.photo FROM items 
  AS si LEFT JOIN categories AS c on c.cat_id = si.cat_id 
  WHERE si.itemid = '".$safe_item_id."'";
 $get_item_res = mysqli_query($mysqli, $get_item_sql) 
 or die(mysqli_error($mysqli)); 

 if (mysqli_num_rows($get_item_res) < 1) { 
 //invalid item 
 $display_block .= "<p><em>Invalid item selection.</em></p>"; 
 } else { 
 //valid item, get info
    while ($item_info = mysqli_fetch_array($get_item_res)) {
        $cat_id = $item_info['cat_id'];
        $cat_title = strtoupper(stripslashes($item_info['cat_name']));
        $item_title = stripslashes($item_info['itemname']);
        $item_price = $item_info['price'];
        $item_desc = stripslashes($item_info['descripition']);
        $item_image = $item_info['photo'];
    }

    //make breadcrumb trail & display of item
    $display_block .= <<<END_OF_TEXT
    <p><em>You are viewing:</em><br/>
    <strong><a href="seestore.php?cat_id=$cat_id">$cat_title</a> &gt; $item_title</strong></p>
    <div style="float: left;"><img src="$item_image" alt="$item_title" /></div>
    <div style="float: left; padding-left: 12px">
    <p><strong>Description:</strong><br/>$item_desc</p>
    <p><strong>Price:</strong> $$item_price</p>
<form method="POST" action="addtocart.php">
END_OF_TEXT;
 //free result 
 mysqli_free_result($get_item_res); 

   $display_block .= " 
   <p><label for=\"sel_item_qty\">Select Quantity:</label> 
   <select id=\"sel_item_qty\" name=\"sel_item_qty\">"; 

   for($i=1; $i<11; $i++) { 
   $display_block .= "<option value=\"".$i."\">".$i."</option>";
   } 
   $display_block .=<<<ENDOFTEXT
    </select><p>
      <input type="hidden" name="sel_item_id"  value="$_GET[itemid]" />
    <button type="submit" name="submit" value="submit">Add to Cart</button>
    </form>
   </div>
ENDOFTEXT;
 }
 return $display_block; 
  //close connection to MySQL 
  mysqli_close($mysqli); 
  }
  ?>

试试这个
// 添加信息到购物车 table

$sql = "Select session_id, sel_item_id from shooppertrack where session_id = $sid and sel_item_id = $si_id";

$res = mysqli_query($mysqli, $sql);

if(mysqli_num_rows($res) == 1)
{
//   update with qty +1;
}else{
$addtocart_sql = "INSERT INTO shooppertrack
                (session_id, sel_item_id, sel_item_qty,
                        date_added)
                VALUES ('".$_COOKIE['PHPSESSID']."',
                '".$safe_sel_item_id."',
                '".$safe_sel_item_qty."',
                now())";
                $addtocart_res = mysqli_query($mysqli, $addtocart_sql)
                or die(mysqli_error($mysqli));
}

您需要在插入之前查询您的购物车以查找该商品和会话。如果存在,您应该更新数量。

$sql = 'select sel_item_qty from shooppertrack where sel_item_id = ' .
       $safe_sel_item_id . ' and session_id = ' $_COOKIE['PHPSESSID'];

$res = mysqli_query($mysqli, $sql);

// does quantity exist?
if (mysqli_num_rows($res) > 0 ) { 
    // get sel_item_qty, add 1, run update query
} else {
    // run your insert query
}