Current location: Hot Scripts Forums » Programming Languages » PHP » MySQL insert record if update fails?


MySQL insert record if update fails?

Reply
  #1 (permalink)  
Old 01-04-06, 10:58 PM
perleo perleo is offline
Coding Addict
 
Join Date: Jul 2003
Location: Ireland
Posts: 269
Thanks: 0
Thanked 0 Times in 0 Posts
MySQL insert record if update fails?

Can mysql add a record if the update query fails?

Heres what I have. I want to do an update of data on record with a id of X, so I do this..

Code:
 UPDATE table SET val = '5' WHERE id = '1'
but what if id = 1 does not exist? Can i put something into that query that will create that entry if it doesnt exist ?
Reply With Quote
  #2 (permalink)  
Old 01-04-06, 11:41 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
You could do:
PHP Code:

<?php


$count 
mysql_num_rows(mysql_query("SELECT id FROM table WHERE id = 1"));

if (
$count == 1)
mysql_query("UPDATE table SET val = '5' WHERE id = 1");
else
mysql_query("INSERT INTO table...");

?>
Reply With Quote
  #3 (permalink)  
Old 01-05-06, 07:51 AM
YourPHPPro's Avatar
YourPHPPro YourPHPPro is offline
Community VIP
 
Join Date: Aug 2003
Posts: 430
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by perleo
Can mysql add a record if the update query fails?
What version of mySQL are you using ? If you are using mySQL 5.x you should check out triggers:
http://www.onlamp.com/pub/a/onlamp/2.../triggers.html
http://forums.mysql.com/list.php?99
Reply With Quote
  #4 (permalink)  
Old 01-06-06, 02:21 PM
dennispopel dennispopel is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 263
Thanks: 0
Thanked 0 Times in 0 Posts
MySQL has a neat feature, but to be used with care:

REPLACE INTO table(id, field1, field2, ...) VALUES(1, f1, f2, ...)

This needs id to be primary key column. If MySQL finds the record with id=1 it will do two things:
1. DELETE the row
2. INSERT the row with the same id.

What to be noted - of you don't specify ALL fields then you will lose existing fields in that row. In short, this mean that this command does not update listed fields, rather it will delete the row and insert fields which have been specified in the command.
__________________
onPHP5.com - PHP5: Articles, News, Tutorials, Interviews, Software and more
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Auto Dealer Script nuzzle PHP 17 04-14-10 08:34 PM
Problem with a mysql table update lppa2004 PHP 2 12-24-05 05:11 AM
insert record loop bhinkle ASP 3 05-11-05 09:04 AM
UPDATE: MySQL Auto Backup & Export v1.1 Beyonder General Advertisements 2 03-21-05 02:05 PM
Problem with Insert in MySql bionicsamir PHP 2 01-02-05 08:08 PM


All times are GMT -5. The time now is 06:26 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.