Jump to content

Update or insert record


mouli

Recommended Posts

Hi
I'm reposting this because I think it got buried and I didn't get a useful reply so I thought I'd try again.
I have a page that uploads images to a database. Each record has an id number and 4 images. When I upload an image I'd like to check whether the id already exists, in which case I simply update that record with the new image, or if the id doesn't exist i would insert a new record.
I figure I can do this by querying the database for the id then test the results for the existance of the id then use another query to insert or update the record but I just wondered whether there's a smoother way to do it such that there is only one query that inserts or updates depending on the existance in the database of the id number.
Many thanks
Link to comment
Share on other sites

[!--quoteo(post=353397:date=Mar 10 2006, 10:24 AM:name=AV1611)--][div class=\'quotetop\']QUOTE(AV1611 @ Mar 10 2006, 10:24 AM) [snapback]353397[/snapback][/div][div class=\'quotemain\'][!--quotec--]
IF I say your request back this way, is it still true?

You want to update the links to some pictures if they already exist, or add them if they don't...

Is that right?
[/quote]
Nearly true...
I want to upload images to a database with a code number for the record. When I upload an image I specify the code number of the record I want the image added to.
For instance I want to upload 45.jpg to field 'webpic1' in record with 'id' = L203
If 'id'= L203 exists then I want it to simply update 'webpic1' with 45.jpg
If 'id'=L203 doesn't exist then I want it to insert a new record with 'id'=L203 with 'webpic1'=45.jpg

I can do it by querying the database for the existance of 'id'=L203, then using an IF statement to decide whether to insert or update but I was thinking there must be a quicker, slicker way to do it.

Any ideas much appreciated.

mouli
Link to comment
Share on other sites

Can't you just do a:

INSERT .......
ON DUPLICATE KEY UPDATE .......

???

check out

[a href=\"http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html\" target=\"_blank\"]http://dev.mysql.com/doc/refman/5.0/en/ins...-duplicate.html[/a]

for more syntax info on how that might work for you..

--RobBob

[!--quoteo(post=353407:date=Mar 9 2006, 02:57 PM:name=mouli)--][div class=\'quotetop\']QUOTE(mouli @ Mar 9 2006, 02:57 PM) [snapback]353407[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Nearly true...
I want to upload images to a database with a code number for the record. When I upload an image I specify the code number of the record I want the image added to.
For instance I want to upload 45.jpg to field 'webpic1' in record with 'id' = L203
If 'id'= L203 exists then I want it to simply update 'webpic1' with 45.jpg
If 'id'=L203 doesn't exist then I want it to insert a new record with 'id'=L203 with 'webpic1'=45.jpg

I can do it by querying the database for the existance of 'id'=L203, then using an IF statement to decide whether to insert or update but I was thinking there must be a quicker, slicker way to do it.

Any ideas much appreciated.

mouli
[/quote]
Link to comment
Share on other sites

[!--quoteo(post=353422:date=Mar 10 2006, 11:54 AM:name=RobBob)--][div class=\'quotetop\']QUOTE(RobBob @ Mar 10 2006, 11:54 AM) [snapback]353422[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Can't you just do a:

INSERT .......
ON DUPLICATE KEY UPDATE .......

???

check out

[a href=\"http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html\" target=\"_blank\"]http://dev.mysql.com/doc/refman/5.0/en/ins...-duplicate.html[/a]

for more syntax info on how that might work for you..

--RobBob
[/quote]

Yup I reckon that might do the job and I learn something new as well.
many thanks RobBob

mouli
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.