mouli Posted March 8, 2006 Share Posted March 8, 2006 HiI 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 Quote Link to comment https://forums.phpfreaks.com/topic/4471-update-or-insert-field-depending-on-id/ Share on other sites More sharing options...
txmedic03 Posted March 8, 2006 Share Posted March 8, 2006 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 Link to comment https://forums.phpfreaks.com/topic/4471-update-or-insert-field-depending-on-id/#findComment-15535 Share on other sites More sharing options...
mouli Posted March 8, 2006 Author Share Posted March 8, 2006 [!--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 JamesThe 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 thanksmouli Quote Link to comment https://forums.phpfreaks.com/topic/4471-update-or-insert-field-depending-on-id/#findComment-15550 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.