我目前正在购买购物车。 我想防止它在购物车上有重复的项目。 例如: 如果我点击“熊猫”,购物车中的数量将为1。 如果我再次点击“熊猫”,数量将变为2,而不是购物车中有2个“熊猫”商品。 有人可以指导我吗? 我是PHP的新手,我们将非常感谢您的帮助。 谢谢
这是我的代码。
details.php
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$status = $this->checkProductStatus($yourdata)
if ($status){
// write your update query
$updateSQL = sprintf("UPDATE supermarketcart SET qty=%s WHERE id=%s",
GetSQLValueString($_POST['name'], "text"),
GetSQLValueString($_POST['description'], "text"),
GetSQLValueString($_POST['package'], "text"),
GetSQLValueString($_POST['price'], "double"),
GetSQLValueString($_POST['imagename'], "text"),
GetSQLValueString($_POST['username'], "text"));
}
else{
//write your insert query
$insertSQL = sprintf("INSERT INTO supermarketcart (pname, pdescription, package, pprice, pimage, username) VALUES (%s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['name'], "text"),
GetSQLValueString($_POST['description'], "text"),
GetSQLValueString($_POST['package'], "text"),
GetSQLValueString($_POST['price'], "double"),
GetSQLValueString($_POST['imagename'], "text"),
GetSQLValueString($_POST['username'], "text"));
mysql_select_db($database_MyDatabase, $MyDatabase);
$Result1 = mysql_query($insertSQL, $MyDatabase) or die(mysql_error());
$insertGoTo = "cart.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
$colname_supermarketDetails = "-1";
if (isset($_GET['desc_sel'])) {
$colname_supermarketDetails = $_GET['desc_sel'];
}
mysql_select_db($database_MyDatabase, $MyDatabase);
$query_supermarketDetails = sprintf("SELECT * FROM supermarket WHERE `description` = %s", GetSQLValueString($colname_supermarketDetails, "text"));
$supermarketDetails = mysql_query($query_supermarketDetails, $MyDatabase) or die(mysql_error());
$row_supermarketDetails = mysql_fetch_assoc($supermarketDetails);
$totalRows_supermarketDetails = mysql_num_rows($supermarketDetails);
mysql_select_db($database_MyDatabase, $MyDatabase);
$query_cart = "SELECT * FROM supermarketcart";
$cart = mysql_query($query_cart, $MyDatabase) or die(mysql_error());
$row_cart = mysql_fetch_assoc($cart);
$totalRows_cart = mysql_num_rows($cart);
$colname_supermarketInfo = "-1";
if (isset($_GET['id'])) {
$colname_supermarketInfo = $_GET['id'];
}
mysql_select_db($database_MyDatabase, $MyDatabase);
$query_supermarketInfo = sprintf("SELECT * FROM supermarket WHERE `description` = %s", GetSQLValueString($colname_supermarketInfo, "text"));
$supermarketInfo = mysql_query($query_supermarketInfo, $MyDatabase) or die(mysql_error());
$row_supermarketInfo = mysql_fetch_assoc($supermarketInfo);
}
public function checkProductStatus($data){
// check the if the product already exist against that user
return true
else false
}
答案 0 :(得分:0)
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")){
$status = $this->checkProductStatus($yourdata)
if ($status){
// write your update query
}
else{
//write your insert query
}
}
public function checkProductStatus($data){
// check the if the product already exist against that user
return true else false
}