guslw
guslw

Reputation: 11

MYSQL, Insert/Update, if else statement

I'm using PHP with mysqli and in this example I'm trying to update the price if the username and lastname exists in the database, else I want to insert a new user. Why is this not working?

IF (SELECT name,lastname FROM peoplePrice WHERE name='gus' AND lastname='lw') THEN
    UPDATE peoplePrice SET price='20' WHERE name='gus' AND lastname='lw';
ELSE
    INSERT INTO peoplePrice (name,lastname,price) 
    VALUES ('nisse','johansson','20');
END IF;

Upvotes: 1

Views: 75

Answers (1)

O. Jones
O. Jones

Reputation: 108641

MySQL doesn't offer the kind of conditional logic you're using except in stored procedures. You should investigate INSERT ... ON DUPLICATE KEY UPDATE to do what you're trying to do. Read this: http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html

Upvotes: 2

Related Questions