How do I know whether to update my database of insert a new row?
If someone logs into my site I want to check to see if they already exist in my database if they exist I want to update the user's info and if not I want to insert new user info.
Is this just a simple "if else" in PHP or are there MySQL methods to take care of this kind of thing?
Thanks
UPDATE or INSERT
Moderator: General Moderators
UPDATE or INSERT
Last edited by jwerre on Wed Feb 11, 2009 3:36 pm, edited 1 time in total.
Re: UPDATE or INSERT
You can use INSERT ... ON DUPLICATE KEY UPDATE
This requires having a unique key on the field you are searching by.
Read the manual for more details - http://dev.mysql.com/doc/refman/5.0/en/ ... icate.html
This requires having a unique key on the field you are searching by.
Read the manual for more details - http://dev.mysql.com/doc/refman/5.0/en/ ... icate.html
Re: UPDATE or INSERT
Code: Select all
mysql_query(" INSERT INTO tb_user (user_id, is_app_added, name_first, name_last, email)
VALUES ($user,'$has_added_app','$first_name','$last_name','$email')
ON DUPLICATE KEY UPDATE email = '$email' ");