1

im trying to create a simple SQL INSERT otherwise UPDATE statement but to be honest I don't know what im doing

here is my function so far

function addInventoryBook($isbn, $sku, $price, $quantity, $condition, $dateOpened){
   $q = "INSERT INTO ".TBL_INVENTORY." VALUES('$isbn', '$sku', $price, 
   $quantity, '$condition', $dateOpened)
   ON DUPLICATE KEY UPDATE VALUES('$isbn', '$sku', $price, $quantity, 
   '$condition', $dateOpened)";     

   return mysql_query($q, $this->connection);           
}

a previous function which seemed that update the price field was working

function addInventoryBook($isbn, $sku, $price, $quantity, $condition, $dateOpened){
   $q = "INSERT INTO ".TBL_INVENTORY." VALUES('$isbn', '$sku', $price, 
   $quantity, '$condition', $dateOpened)
   ON DUPLICATE KEY UPDATE price = $price";     

   return mysql_query($q, $this->connection);           
}
4
  • 1
    try ON DUPLICATE KEY UPDATE SET price = VALUES(price), sku = VALUES(sku) ,quantity = VALUES(quantity), ....., you might need to add the field names between ".TBL_INVENTORY." and VALUES. Commented Jul 24, 2012 at 18:06
  • echo your query before you run it, make sure it's actually what you think it is. Commented Jul 24, 2012 at 18:14
  • @Crontab good point thats happened before Commented Jul 24, 2012 at 18:16
  • There is no "SET" anywhere as part of the correct mySQL syntax. (It was also in the original question and then fixed.) Commented Jul 24, 2012 at 18:24

2 Answers 2

5

I'd write it like this, rather than supplying each of the values a second time:

ON DUPLICATE KEY UPDATE 
  isbn       = VALUES(isbn)
  sku        = VALUES(sku)
  price      = VALUES(price)
  quantity   = VALUES(quantity)
  condition  = VALUES(condition)
  dateOpened = VALUES(dateOpened)

(That will assign the values that would have been inserted, if the insert had succeeded.)

(You likely don't want assign values to whichever columns make up the "unique" key that was violated by the insert; I've included all the columns here because that's what you show in your attempt.)

Sign up to request clarification or add additional context in comments.

1 Comment

@Vatev: very good point about this pattern working for statements that insert multiple rows. (This answer is the same answer you left as a comment; I upvoted your comment, I would upvote your answer if you leave one.)
2

Assuimg TBL_INVENTORY is a string with the column values:

$q = "INSERT INTO ".TBL_INVENTORY." VALUES('$isbn', '$sku', $price,     $quantity, '$condition', $dateOpened) ON DUPLICATE KEY UPDATE isbn='$isbn', sku='$sku', price='$price', quantity='$quantity', condition='$condition', dateOpened='$dateOpened'";

Reference link : http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

4 Comments

You don't have to include every single field in the update portion of the query.
My point is maybe the asker only would want to update the price in an update condition.
you're right, I just did it for consistency. @mk_89 will use it as he/she pleases. I thought you meant there was a sql function go around this..
I think I have tried this before and I remember that it does not work because I could not update more than 3 fields this way, I usually get the same problem when using OR statements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.