Page 1 of 1

INSERT question

Posted: Tue May 24, 2005 9:55 pm
by harrisonad
how can i insert a record into database if the record doesn't exist. I want to do this in a single query. Please give me a sample query. Thanks.

Posted: Tue May 24, 2005 9:58 pm
by AISBERG
INSERT INTO `tablename` (`...`) VALUES ('...')

you'd better check the php manual
http://www.php.net/manual i think

Posted: Tue May 24, 2005 10:01 pm
by Skara
*googles*

well, there's no IF NOT EXISTS kind of thing, but something like this might work:

Code: Select all

INSERT INTO tb (firstname, lastname) VALUES ('Jack', 'Doe') ON DUPLICATE KEY UPDATE lastname = lastname;

Posted: Tue May 24, 2005 10:22 pm
by harrisonad
I use this code but it gives mysql error:

Code: Select all

$query = "INSERT INTO directory (fname,lname) VALUES ('$fname','$lname') WHERE concat(fname,' ',lname)!='$fname $lname'";