Mostafa Baezid Mostafa Baezid - 2 months ago 11
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

userinfo
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:

<?php
include 'sql.php';
if(isset($_GET['delete_id']))
{
// 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);

unlink("assets/img/user_images/".$db_field['user_pic']);
//delete from db
mysql_query("DELETE FROM userinfo WHERE user_id=".$_GET['delete_id']);

if($select_img===false){
die(mysql_error());
}

else
{?>
<script>
window.alert('Delete images Success!!!');
</script>
<?php
header("refresh:2;user_profile_settings.php");
}
}

?>
// 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

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']'";
mysql_query($query);

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