Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC Microsoft Access Driver] The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again., SQL state 23000 in SQLExecDirect in C:\XAMPP\xampp\htdocs\Shopping_Cart_code.php on line 53
Code (PHP)
<?php
$conn=odbc_connect ('ac','','');
if (!$conn)
{
exit("Connection Failed: " . $conn);
}
if(isset($_COOKIE['Shopping_Cart']))
{
$sql="SELECT * FROM Shopping_Cart ORDER BY id DESC";
$rs=odbc_exec($conn,$sql);
$rowShopping=odbc_fetch_array($rs);
$idCart=$rowShopping["id"];
//echo $_COOKIE['Shopping_Cart'];
}else{
$sql="INSERT INTO Shopping_Cart (SumPrice) VALUES (0)";
$rs=odbc_exec($conn,$sql);
$sql="SELECT * FROM Shopping_Cart ORDER BY id DESC";
$rs=odbc_exec($conn,$sql);
$rowShopping=odbc_fetch_array($rs);
$idCart=$rowShopping["id"];
setcookie("Shopping_Cart",$idCart,time()+24*30*3600);
//echo "Create cookie";
}
$conn=odbc_connect ('ac','','');
if (!$conn)
{
exit("Connection Failed: " . $conn);
}
$idProduct=$_GET["pid"];
$sql="SELECT * FROM Shopping_Cart2 WHERE ProductID=".$idProduct." AND idShopping=".$idCart;
$rs=odbc_exec($conn,$sql);
$flag=false;
while($row=odbc_fetch_array($rs))
{
$flag=true;
}
if(!$flag)
{
$sql="SELECT * FROM Product WHERE ProductID=".$idProduct;
$rs=odbc_exec($conn,$sql);
$rowProduct=odbc_fetch_array($rs);
$sum=$rowShopping["SumPrice"]+$rowProduct["ProductPrice"];
$sql="UPDATE Shopping_cart SET SumPrice=".$sum." WHERE id=".$idCart;
$rs=odbc_exec($conn,$sql);
/*$sql="SELECT id FROM Shopping";*/
$sql="INSERT INTO Shopping_Cart2 (idShopping,ProductID,amount) VALUES ('$idCart','$idProduct',1)";
$rs=odbc_exec($conn,$sql);
}
odbc_close($conn);
require("Shopping_Cart.php");
?>