drayarms Posted May 13, 2011 Share Posted May 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236301-update-query-inserts-instead-of-update/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 13, 2011 Share Posted May 13, 2011 $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. Quote Link to comment https://forums.phpfreaks.com/topic/236301-update-query-inserts-instead-of-update/#findComment-1214927 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.