Jump to content


Photo

update or insert field depending on id


  • Please log in to reply
2 replies to this topic

#1 mouli

mouli
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 08 March 2006 - 08:57 PM

Hi
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 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 08 March 2006 - 09:19 PM

Well, my number one question is, why do you have four images per row? It would make things simpler to use seperate rows for each image unless there is a really good reason for grouping those 4 images. Even then you could assign some kind of grouping tag. I only say this because it may make things simpler for you. I don't know the purpose for doing what you did, so I can't say you did something good/bad here, but I am curious as to the reasoning.

The next thing I would like to know is why you are defining the id outside of the mysql database? I just wonder why you do not let mysql auto_increment the value of id where id is the primary unique identifier. You could do something to where if image is being re-uploaded it passes a variable to the $_POST or $_GET with the id number of the image you want to re-upload/replace then if there was no id specified it creates a new record. You could use other error correcting like if your queries fail alert the user or take some other action, but this is just what seems to be simplest to me. Again, I don't know exactly what you are trying to accomplish and what you want to be able to do with the images once you have them in the database, so I am just passing along suggestions. I hope it helps.

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.


#3 mouli

mouli
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 08 March 2006 - 09:37 PM

[!--quoteo(post=352997:date=Mar 9 2006, 10:19 AM:name=txmedic03)--][div class=\'quotetop\']QUOTE(txmedic03 @ Mar 9 2006, 10:19 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Well, my number one question is, why do you have four images per row? It would make things simpler to use seperate rows for each image unless there is a really good reason for grouping those 4 images. Even then you could assign some kind of grouping tag. I only say this because it may make things simpler for you. I don't know the purpose for doing what you did, so I can't say you did something good/bad here, but I am curious as to the reasoning.

The next thing I would like to know is why you are defining the id outside of the mysql database? I just wonder why you do not let mysql auto_increment the value of id where id is the primary unique identifier. You could do something to where if image is being re-uploaded it passes a variable to the $_POST or $_GET with the id number of the image you want to re-upload/replace then if there was no id specified it creates a new record. You could use other error correcting like if your queries fail alert the user or take some other action, but this is just what seems to be simplest to me. Again, I don't know exactly what you are trying to accomplish and what you want to be able to do with the images once you have them in the database, so I am just passing along suggestions. I hope it helps.
[/quote]
Hi James
The website is selling items. Each item has a stock code (the id mentioned above) and has 4 images of the item so having them all in one record with the unique product id makes sense to me.
The user uploads an image and specifies the product id. If the product id exists then it only needs to update the images for that product but if id doesn't exist then it inserts a new record with the new image.
What I want to do is check for the existance of the id so i can then update or insert as required.

many thanks

mouli




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users