English 中文(简体)
MySQL没有更新重复的关键
原标题:MySQL not updating on duplicate key

然而,我担心的是:

$size = isset($_POST[ size ])
  ? " ".mysql_real_escape_string($_POST[ size ])." "
  : "NULL";

$color = isset($_POST[ color ])
  ? " ".mysql_real_escape_string($_POST[ color ])." "
  : "NULL";

if (is_numeric($_POST[ productID ]) && is_numeric($_POST[ amount ]))
{           
  mysql_query("INSERT INTO usercart VALUES (  ,  "
  .mysql_real_escape_string($_SESSION[ user_ID ])." ,  "
  .mysql_real_escape_string($_POST[ productID ])." , "
  .$size." , ".$color." ,  ".mysql_real_escape_string($_POST[ amount ])." )"
  ." ON DUPLICATE KEY UPDATE amount = amount +  "
  .mysql_real_escape_string($_POST[ amount ])." ") or die(mysql_error());
}

在我的我的SQL表一中,有一个独特的索引,名为product>,userID,size and color。 但是,当我提出疑问时,它只是插入而不是更新数额。 阅读MySQL文件,我可以而且不应该具体指明指数名称。

现在需要几小时左右。 因此,谁知道什么?

最佳回答

NULL is always considered unique. So if you set either color or size to null the tuple you re inserting will be considered unique as far as your unique constraints are concerned.

问题回答

It appears you have a surrogaat key "cart_id", which is also auto increment as the first field. Your fragment will probably result in the DBMS trying to enter user_id into the first field, which is cart_id. Try using a designated insert, omitting the cart_id, like:

INSERT INTO usercart(user_id, product_id, ... )
VALUES (user_id, product_id, ...)
   ;




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签