Jump to content

[SOLVED] PHP/MYSQL Insert Into but Update if exists...how ???


nightkarnation

Recommended Posts

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!

 

Link to comment
Share on other sites

$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.

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.