Hi, I’m having trouble with this query:
$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());
}
In my MySQL table I have a unique index named "product" on productID, userID, size and color. But when I run this query it just inserts instead of updating the amount. Reading the MySQL documentation I can and should not have to specify the index name.
Been fiddling around with this for hours now. So anyone knows whats up?
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.