0

This is my database of table cart when I add product to my cart table then error occurs

Database
mysql_query($query, $db) or die(mysql_error($db));
$query = 'CREATE TABLE IF NOT EXISTS ecomm_temp_cart (
session CHAR(50) NOT NULL,
product_code CHAR(5) NOT NULL,
qty INTEGER UNSIGNED NOT NULL,
PRIMARY KEY (session, product_code),
FOREIGN KEY (product_code) REFERENCES ecomm_products(product_code)
)
ENGINE=MyISAM';

my product table

mysql_query($query, $db) or die(mysql_error($db));
$query = 'CREATE TABLE IF NOT EXISTS ecomm_products (
product_code CHAR(5) NOT NULL,
name VARCHAR(100) NOT NULL,
description MEDIUMTEXT,
price DEC(6,2) NOT NULL,
PRIMARY KEY(product_code)
)
ENGINE=MyISAM';


<?php
session_start();
require 'db.inc.php';//connection to database
?>
<html>
<head>
<title>Here is Your Shopping Cart!</title>
<style type="text/css">
  th { background-color: #999;}
  td { vertical-align: top; }
  .odd_row { background-color: #EEE; }
  .even_row { background-color: #FFF; }
 </style>
</head>
<body>
<h1>Comic Book Appreciation Store</h1>
<?php
  $db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or
die ('Unable to connect. Check your connection parameters.');
mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db)); 
$session = session_id();
$query = 'SELECT
    t.product_code, qty,
    name, description, price
FROM
    ecomm_temp_cart t JOIN ecomm_products p ON
    t.product_code = p.product_code
WHERE
    session = "' . $session . '"
ORDER BY
    t.product_code ASC';
 $result = mysql_query($query, $db) or die (mysql_error($db));

 $rows = mysql_num_rows($result);
if ($rows == 1) 
 {
  echo '<p>You currently have 1 product in your cart.</p>';
 } 
else 
  {
    echo '<p>You currently have ' . $rows . ' products in your cart.</p>';
   }
 if ($rows > 0) 
   {
 ?>
 <table style="width: 75%;">
 <tr>
 <th style="width: 100px;"></th><th> Item Name </th><th> Quantity </th>
 <th> Price Each </th><th> Extended Price </th>
 </tr>
 <?php
$total = 0;
$odd = true;
while ($row = mysql_fetch_array($result)) 
{
   echo ($odd == true) ? '<tr class="odd_row">' : '<tr class="even_row">';
     $odd = !$odd;
     extract($row);
?>
 <td style="text-align:center;"><a href="ecomm_view_product.php?product_code=<?php
 echo $product_code; ?>"><img src="images/<?php echo $product_code;
 ?>_t.jpg"
alt="<?php echo $name; ?>"/></a></td>
  <td><a href="ecomm_view_product.php?product_code=<?php echo $product_code;
 ?>"><?php
echo $name; ?></a></td>
<td>
<form method="post" action="ecomm_update_cart.php">
<div>
  <input type="text" name="qty" maxlength="2" size="2"
   value="<?php echo $qty; ?>"/>
    <input type="hidden" name="product_code"
    value="<?php echo $product_code; ?>"/>
  <input type="hidden" name="redirect" value="ecomm_view_cart.php"/>
  <input type="submit" name="submit" value="Change Qty"/>
</div>
</form>
</td>
<td style="text-align: right;"> $<?php echo $price; ?></td>
<td style="text-align: right;"> $<?php echo number_format
($price * $qty, 2); ?>
</td>
</tr>
<?php
   $total = $total + $price * $qty;
 }
?>
</table>
<p> Your total before shipping is:
 <strong>$<?php echo number_format($total, 2); ?></strong></p>
<form method="post" action="ecomm_checkout.php">
<div>
<input type="submit" name="submit" value="Proceed to Checkout" style="font-       weight: bold;"/>
</div>
</form>
<form method="post" action="ecomm_update_cart.php">
 <div>
<input type="hidden" name="redirect" value="ecomm_shop.php"/>
<input type="submit" name="submit" value="Empty Cart"/>
 </div>
 </form>
 <?php
  }
  ?>
  <hr/>
  <p><a href="ecomm_shop.php"><< Back to main page </a></p>
  </body>
  </html>

I have created a product web page and when I add quantity and click on add to cart then it shows duplicate error . I try to fix it but can't fix it..

 > My update cart

 <?php
 require 'db.inc.php';
 $db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or
 die ('Unable to connect. Check your connection parameters.');
 mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db));
 session_start();
 $session = session_id();
  $qty = (isset($_POST['qty']) && ctype_digit($_POST['qty'])) ?  $_POST['qty'] : 0;
$product_code = (isset($_POST['product_code'])) ? $_POST['product_code'] : '';
$action = (isset($_POST['submit'])) ? $_POST['submit'] : '';
$redirect = (isset($_POST['redirect'])) ? $_POST['redirect'] :   'ecomm_shop.php';
 switch ($action) 
{
case 'Add to Cart':
    if (!empty($product_code) && $qty > 0) {
    $query = 'INSERT INTO ecomm_temp_cart(session, product_code, qty)
    VALUES
    ("' . $session . '", "' .
    mysql_real_escape_string($product_code, $db) . '", ' . $qty . ')';
    mysql_query($query, $db) or die(mysql_error($db));
}
header('Location: ' . $redirect);
exit();
break;
case 'Change Qty':
if (!empty($product_code)) {
if ($qty > 0) {
$query = 'UPDATE ecomm_temp_cart
    SET
qty = ' . $qty . '
    WHERE
session = "' . $session . '" AND
product_code = "' .
mysql_real_escape_string($product_code, $db) . '"';
} else {
$query = 'DELETE FROM ecomm_temp_cart
WHERE
session = "' . $session . '" AND
product_code = "' .
mysql_real_escape_string($product_code, $db) . '"';
}
mysql_query($query, $db) or die(mysql_error($db));
}
header('Location: ' . $redirect);
exit();
break;
case 'Empty Cart':
$query = 'DELETE FROM ecomm_temp_cart
 WHERE
 session = "' . $session . '"';
mysql_query($query, $db) or die(mysql_error($db));
header('Location: ' . $redirect);
exit();
 break;
 }
?>
kkishor
  • 3
  • 4
  • I'd normally expect that kind of error when doing an INSERT or an UPDATE, but there don't seem to be either in the code you've provided. – Jonnix May 05 '15 at 15:53
  • Obligatory *please stop using the deprecated mysql_ extension* note : http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – CD001 May 05 '15 at 16:00
  • Please reply once how to solve it. I have changed insert to update but didn't get fix. so kindly help me to edit. i have given my update page please have a look at once and answer me it's urgent please help me. – kkishor May 08 '15 at 06:29

1 Answers1

0

Your answer is signifying you already have that primary key in the table.

The primary key of the table ecomm_temp_cart is (session, product_code). So you already have a row with that session and product_code.

If you are trying to update the quantity, you should be using REPLACE instead of INSERT or simply an UPDATE statement. REPLACE can be a drop in replacement for INSERT and will delete the existing row and insert the new row, effectively overwriting it.


Another possibility is you are not using a valid product_code. From your error, the product_code is 0000. If that isn't correct, then you're probably using the default product_code for each insert.

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
  • Please reply once how to solve it. I have changed insert to update but didn't get fix. so kindly help me to edit. i have given my update page please have a look at once and answer me it's urgent please help me. – kkishor May 06 '15 at 06:31