键'PRIMARY的重复输入

时间:2015-05-05 15:49:32

标签: php mysql

当我将产品添加到购物车表时,这是我的购物车数据库,然后发生错误

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';

我的产品表

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>

我创建了一个产品网页,当我添加数量并点击添加到购物车时,它会显示重复的错误。我试着解决它,但无法解决它..

 > 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;
 }
?>

1 个答案:

答案 0 :(得分:0)

您的回答表明您已经在表格中拥有该主键。

ecomm_temp_cart的主键是(session, product_code)。因此,您已经拥有会话 product_code 的行。

如果您要更新数量,则应使用REPLACE代替INSERT或仅使用UPDATE语句。 REPLACE可以替代INSERT,并删除现有行并插入新行,从而有效地覆盖它。

另一种可能性是您没有使用有效的product_code。根据您的错误,product_code为0000。如果这不正确,那么您可能正在为每个插入使用默认的product_code。