Jump to content

delete images from database


Alidad

Recommended Posts

hi, I'm kind of confused and rookie about the delete image file name from database, I have five images file name  in database  and store image file in the folder, each table column name is  (image_1, image_2, images_3, image_4, image_5).

 
In html , each image have delete button under bottom of image for up to five images.
 
Let say that I want to delete 'image_3" by pressing button, how can i write that code to make sure that i can delete that images file name ONLY from database instead of delete rest of row!
 
i wrote code but not sure working right...
button 
 
<a href='inventory_list.php?deleteid=$id'>delete</a>
 
<?php 
 
 
if (isset($_GET['yesdelete_image_3'])) {
// remove item from system and delete its picture
// delete from database
$id_to_delete = $_GET['yesdelete'];
$sql = mysql_query("DELETE FROM products WHERE id='$id_to_delete' LIMIT 1") or die (mysql_error());
// unlink the image from server
// Remove The Pic -------------------------------------------
    $pictodelete = ("../inventory_images/$id_to_delete.jpg");
    if (file_exists($pictodelete)) {
            unlink($pictodelete);
    }
header("location: inventory_list.php"); 
    exit();
}
?>

 

 

 

 

Link to comment
Share on other sites

Before you waste time getting this to work...

 

1)

Use $_POST instead of $_GET

Filter and check your POST variables too... so when they delete the pic they don't take the whole database too.

And don't use Superglobals in your code:

Do something like this instead...

if (filter_input(INPUT_POST, 'list_direction', FILTER_SANITIZE_STRING) === NULL) {
$posted['list_direction'] = '';
} else {
$posted['list_direction'] = filter_input(INPUT_POST, 'list_direction', FILTER_SANITIZE_STRING);
}
if (filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT) === NULL) {
$posted['id'] = '';
} else {
$posted['id'] = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT);
}

2)

don't use mysql it is depreciated use mysqli or pdo

 

3)

Use a different database / coding approch for storing and getting your images...

Having column names as each image will get out of hand.

Especially if you decide to add more pictures per user or page.

 

Set a user or whatever you decide, then associate the pic based on the user Id

 

Then you can use a POST to see if the delete button has been pushed with a hidden "id" value

 

Remember we filtered our POST and set to $posted (or whatever you want to name it)

if ($posted['del'] == 'true') {
    $sql = "DELETE FROM info WHERE pic_id = '$posted['id']'";
    $result = $database->query($sql);

etc...
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.