Jump to content


Photo

Update or insert record


  • Please log in to reply
4 replies to this topic

#1 mouli

mouli
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 09 March 2006 - 09:22 PM

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

#2 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 09 March 2006 - 09:24 PM

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?


#3 mouli

mouli
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 09 March 2006 - 09:57 PM

[!--quoteo(post=353397:date=Mar 10 2006, 10:24 AM:name=AV1611)--][div class=\'quotetop\']QUOTE(AV1611 @ Mar 10 2006, 10:24 AM) View Post[/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

#4 rjlowe

rjlowe
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationAlbuquerque

Posted 09 March 2006 - 10:54 PM

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) View Post[/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]


#5 mouli

mouli
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 10 March 2006 - 12:39 AM

[!--quoteo(post=353422:date=Mar 10 2006, 11:54 AM:name=RobBob)--][div class=\'quotetop\']QUOTE(RobBob @ Mar 10 2006, 11:54 AM) View Post[/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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users