我需要知道如何不重复项目并将数量从showitem.php添加到数量
但是我在sql中对表进行了更新,但没有发生在表格中我不知道为什么?
这是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> > $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);
}
?>
答案 0 :(得分:1)
在插入之前,您需要在购物车中查询该项目和会话。如果存在,则应更新数量。
$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
}
答案 1 :(得分:0)
试试这个 //将信息添加到购物车表
$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));
}