Jump to content

Update query inserts instead of update.


drayarms

Recommended Posts

I created this code to upload a member's main picture on his member page on website.  I'll only include the query part of the code since that's what is relevant to my problem.  The idea is basically to upload a new picture onto the database if no picture already exists for that member and display the picture on the page.  If a picture already exists, then the script replaces the old picture with the new one upon upload.    But for whatever reason

I don't understand,  when I try to replace the old pic, it gets inserted in a new row on the database instead of replacing the old row, and the new pic gets displayed on the web page alongside the old.

$query = "SELECT username FROM images WHERE member_id = '".$_SESSION['id']."' AND image_cartegory = 'main'";
        	$result = @mysql_query($query);
        	$num = @mysql_num_rows($result);
       
        	if ($num> 0)   {

   				 //Update the image

    				$update = mysql_query("UPDATE images SET image = '" . $image['name'] . "' WHERE member_id = '".$_SESSION['id']."' AND image_cartegory = 'main'");  

    				
  			        $_SESSION['error'] = "File updated successfully.";  //really should be session success message.

    		                header("Location: member.php");


    				exit;
			}

			else

			{
			// NOTE: This is where a lot of people make mistakes.
			// We are *not* putting the image into the database; we are putting a reference to the file's location on the server

    				$sql = "insert into images (member_id, image_cartegory, image_date, image) values ('{$_SESSION['id']}', 'main', NOW(), '" . $image['name'] . "')";

   		 		$result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());

    				$_SESSION['error'] = "File uploaded succussfully.";  //really should be session success message.

    		                header("Location: member.php");
                                }

 

 

So can anyone tell me what the problem is?  Could the fact that my insert script actually uploads the image onto a folder on my server and only stores the path name in the database have anything to contribute to the mixup?  Appreciate your responses in advance.

Link to comment
Share on other sites

$num is probably not > 0. Have you echoed it or better yet used var_dump() to see what it actually is?

 

Given that you were likely getting php errors on the mysql_query() and mysql_num_rows() statements, so you added the @ to hide the errors, I would guess that your query is failing with to an error of some kind and using mysql_error like you are doing on the insert query would probably tell you why the select query is failing.

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.