mouli Posted March 9, 2006 Share Posted March 9, 2006 HiI'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 Quote Link to comment https://forums.phpfreaks.com/topic/4559-update-or-insert-record/ Share on other sites More sharing options...
AV1611 Posted March 9, 2006 Share Posted March 9, 2006 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 Link to comment https://forums.phpfreaks.com/topic/4559-update-or-insert-record/#findComment-15920 Share on other sites More sharing options...
mouli Posted March 9, 2006 Author Share Posted March 9, 2006 [!--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' = L203If 'id'= L203 exists then I want it to simply update 'webpic1' with 45.jpgIf 'id'=L203 doesn't exist then I want it to insert a new record with 'id'=L203 with 'webpic1'=45.jpgI 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 https://forums.phpfreaks.com/topic/4559-update-or-insert-record/#findComment-15929 Share on other sites More sharing options...
rjlowe Posted March 9, 2006 Share Posted March 9, 2006 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' = L203If 'id'= L203 exists then I want it to simply update 'webpic1' with 45.jpgIf 'id'=L203 doesn't exist then I want it to insert a new record with 'id'=L203 with 'webpic1'=45.jpgI 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] Quote Link to comment https://forums.phpfreaks.com/topic/4559-update-or-insert-record/#findComment-15941 Share on other sites More sharing options...
mouli Posted March 10, 2006 Author Share Posted March 10, 2006 [!--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 RobBobmouli Quote Link to comment https://forums.phpfreaks.com/topic/4559-update-or-insert-record/#findComment-15984 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.