1

How to update value "100" If the $id duplicate by adding +100 to it so the total will be 200 in the level here is my INSERT statement

  INSERT INTO users (id, name, level) VALUES ($id, 'name', 100 ) 

2 Answers 2

2

This should do the trick for you (I am assuming that id is a unique field and can be used to identify the correct row to update):

$query="INSERT INTO 
users (id, name, level) 
VALUES ($id, 'name', 100 ) 
on duplicate key 
    update set level=level+100";
Sign up to request clarification or add additional context in comments.

Comments

0

Since UPDATE is MySQL specific the more generic way is to do INSERT INTO users (id,name,level) VALUES ($id, 'name', 100) ON DUPLICATE KEY UPDATE level = level + VALUES(level)

See http://dev.mysql.com/doc/refman/5.1/de/insert-on-duplicate.html AFAIK this is more generally applicable then only on MySQL.

Comments

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.