Mostafa Baezid Mostafa Baezid - 1 year ago 71
MySQL Question

image unlink from folder but can't delete from database column

I'm learning php and mysql. Right now I need some help with delete image using $_GET method. I can unlink image from folder but not from database column. Here is my database structure for

table columns are-

user_id| username| email| password| department| position| first_name|
last_name| status| user_pic| join_date| modified

Here is my php code:

include 'sql.php';
// select image from db to delete
$select_img = mysql_query("SELECT user_pic FROM userinfo WHERE user_pic
=" .$_GET['delete_id']);

$db_field= mysql_fetch_array($select_img);

//delete from db
mysql_query("DELETE FROM userinfo WHERE user_id=".$_GET['delete_id']);


window.alert('Delete images Success!!!');

// here is the HTML part Which display using while loop
<a class="btn close" href="?delete_id=<?php echo $userpic; ?>" title="click
for delete" onclick="return confirm('sure to delete ?')" >&times;</a>
<img src="assets/img/user_images/<?php echo $userpic; ?>" />

I know this is pretty simple code.. I want to learn thats why I'm posting this. I already tired lots of Technic but every time I failed. Please Help me out with this.
Advance Thanks

Answer Source

DELETE FROM deletes the entire row. I'm pretty sure it refuses because of foreign keys or something.

If you only want to empty the column, you should use UPDATE

$query = "UPDATE userinfo SET user_pic='' WHERE user_id='$_GET['delete_id']'";

You should also listen to what the others are saying and think about switching to MYSQLI, but more importantly, preventing SQL injection!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download