Update query inserts instead of update.

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.

[php]
$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");
                            }

[/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.

<?php session_start(); // From Saif: You have to declare the function session_start(); $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"); // you can add a standard process for showing the success message using get method. like use header("Location: member.php?msg=success"); then add two lines of code like if($_GET['msg'] == "success") { echo "File updated successfully.";} in your member page 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"); } ?>
Sponsor our Newsletter | Privacy Policy | Terms of Service