Alidad Posted December 27, 2013 Share Posted December 27, 2013 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(); } ?> Quote Link to comment Share on other sites More sharing options...
rbrown Posted December 27, 2013 Share Posted December 27, 2013 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... Quote Link to comment 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.