Jump to content

update or insert field depending on id


mouli

Recommended Posts

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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[!--quoteo(post=352997:date=Mar 9 2006, 10:19 AM:name=txmedic03)--][div class=\'quotetop\']QUOTE(txmedic03 @ Mar 9 2006, 10:19 AM) [snapback]352997[/snapback][/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
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.