nightkarnation Posted August 4, 2008 Share Posted August 4, 2008 Ok...i have an application that communicates with flash and enables the user to upload his pictures to in this case (localhost server)... In this part of the application which im stuck...i just want to store in mysql database the name of the user and the count of how many pictures hes uploading... Heres the php code: mysql_query("INSERT INTO `tbl_user_upload` (user_id) VALUES ('$userName')"); mysql_query("UPDATE `tbl_user_upload` SET count = count + 1 WHERE user_id = '$userName'"); Now...lets say that this is the first time the user uploads a picture...everything works fine...on the tbl_user_upload database...the user_id is stored with his name and with the count of 1... But...now that the same user is uploading his 2nd image...obviously because of the code...its creating another field with the same name of the user_id and with the value count of 2. I want it just to update the already created user_id and count value on the database... I need a way to fix this inside this code...this happens when the user uploads his image, sorry for the stupid question but im stuck here, Thanx for ur reply Cheers! Quote Link to comment Share on other sites More sharing options...
.josh Posted August 4, 2008 Share Posted August 4, 2008 $result = mysql_query("selet user_id from tbl_user_upload where user_id = '$userName'"; if (mysql_num_rows($result) > 0) { mysql_query("UPDATE `tbl_user_upload` SET count = count + 1 WHERE user_id = '$userName'"); } else { mysql_query("INSERT INTO `tbl_user_upload` (user_id) VALUES ('$userName')"); } This is a very bad way to actually do this because it's ugly and doesn't handle errors or anything like that, but hopefully you can use this to get the idea. Quote Link to comment Share on other sites More sharing options...
php_dave Posted August 4, 2008 Share Posted August 4, 2008 From a pure Mysql point of view you could use the following: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html INSERT INTO tbl_user_upload (user_id, count) VALUES ('$userName',1) ON DUPLICATE KEY UPDATE count=count+1 obviously user_id would be your key. Quote Link to comment Share on other sites More sharing options...
nightkarnation Posted August 5, 2008 Author Share Posted August 5, 2008 Thanx for the data guys, very useful! Cheers! Quote Link to comment Share on other sites More sharing options...
2tonejoe Posted August 5, 2008 Share Posted August 5, 2008 I am a big fan of making unique indexes and then using the REPLACE INTO cheap and easy Quote Link to comment 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.