INSERT question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

INSERT question

Post 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.
AISBERG
Forum Newbie
Posts: 8
Joined: Sat May 21, 2005 3:17 pm
Location: Bucharest
Contact:

Post by AISBERG »

INSERT INTO `tablename` (`...`) VALUES ('...')

you'd better check the php manual
http://www.php.net/manual i think
Last edited by AISBERG on Tue May 24, 2005 10:01 pm, edited 1 time in total.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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;
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post 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'";
Post Reply