Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-03-2009, 01:44 AM   PM User | #1
ubh
Regular Coder

 
ubh's Avatar
 
Join Date: Apr 2008
Location: Portland, Oregon U.S.A.
Posts: 443
Thanks: 108
Thanked 15 Times in 14 Posts
ubh is on a distinguished road
INSERT INTO only if no record exsist

Hey I am trying to figure out a way to preform either an UPDATE or a INSERT INTO depending on if a row exists or not.

I am running an AJAX process that sends a series of elements inline CSS style values into my database.

Problem is that there should only be one row for each element and then its CSS values should be stored in the proceeding columns. What I get however, because my AJAX process runs in a for loop and loops over all elements, is multiple rows each containing that particular loops sent CSS style.

Looks like this:

Code:
|element_id|width|height|background-color|
------------------------------------------
|first     |null |null  |#000000         |
|first     |null |100px |null            |
|first     |200px|null  |null            |
What I need is to somehow in my php file, try and run a UPDATE process first, and if no row exists then perform an INSERT INTO query.

Anyone know how to conditional a MySQL query like this??

Last edited by ubh; 03-03-2009 at 01:51 AM..
ubh is offline   Reply With Quote
Old 03-03-2009, 01:54 AM   PM User | #2
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,714
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
http://dev.mysql.com/doc/refman/5.0/...duplicate.html
__________________
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.
CFMaBiSmAd is offline   Reply With Quote
Old 03-03-2009, 03:05 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,249
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
The best way to do this is with a Stored Procedure.

You just attempt the UPDATE and, if it fails, then you use an INSERT instead.

Doing this all within a single simple SP is efficient and relatively simple.
Old Pedant is offline   Reply With Quote
Old 03-03-2009, 04:57 AM   PM User | #4
bazz
Master Coder

 
Join Date: Apr 2003
Location: in my house
Posts: 5,211
Thanks: 39
Thanked 201 Times in 197 Posts
bazz will become famous soon enoughbazz will become famous soon enough
try insert ignore.

it will insert if there is no duplicate and will ignore the insert if there would be a duplicate.

bazz
__________________
"The day you stop learning is the day you become obsolete"! - my late Dad.

Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
Useful MySQL resource
Useful MySQL link
bazz is offline   Reply With Quote
Old 03-03-2009, 05:16 AM   PM User | #5
ubh
Regular Coder

 
ubh's Avatar
 
Join Date: Apr 2008
Location: Portland, Oregon U.S.A.
Posts: 443
Thanks: 108
Thanked 15 Times in 14 Posts
ubh is on a distinguished road
Ok maybe its me but I am not understanding how to work with this syntax here.

This is what I assume its trying to come across as, but I still wont work.


PHP Code:
mysql_query("INSERT INTO structureandstyle
(id, $rules, prevSibling, nextSibling) VALUES('$id', '$styles', '$prevSibling','$nextSibling') 
ON DUPLICATE KEY UPDATE structureandstyle SET id='$id', $rule='$rule', prevSibling='$prevSibling', nextSibling='$nextSibling' WHERE id='$id' 
"
)or die(mysql_error()); 

Last edited by ubh; 03-03-2009 at 05:28 AM..
ubh is offline   Reply With Quote
Old 03-03-2009, 07:22 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,249
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
That's only going to work--and Bazz's suggestion is only going to work--*IF* the field that you don't want to duplicate is INDEXED and the index is specified as NO DUPLICATES allowed.

There's nothing "natural" in a database that says you can't have duplicate values. Only a "unique index" can enforce that.
Old Pedant is offline   Reply With Quote
Old 03-03-2009, 07:23 AM   PM User | #7
bazz
Master Coder

 
Join Date: Apr 2003
Location: in my house
Posts: 5,211
Thanks: 39
Thanked 201 Times in 197 Posts
bazz will become famous soon enoughbazz will become famous soon enough
Quote:
Originally Posted by ubh View Post
Code:
mysql_query("INSERT INTO structureandstyle
(id, $rules, prevSibling, nextSibling) VALUES('$id', '$styles', '$prevSibling','$nextSibling') 
ON DUPLICATE KEY UPDATE structureandstyle SET id='$id', $rule='$rule', prevSibling='$prevSibling', nextSibling='$nextSibling' WHERE id='$id' 
")or die(mysql_error());
maybe the red bit should be changed by getting rid of the $.

What error are you getting?
bazz
__________________
"The day you stop learning is the day you become obsolete"! - my late Dad.

Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
Useful MySQL resource
Useful MySQL link
bazz is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:26 AM.


Advertisement
Log in to turn off these ads.