oraya Posted July 12, 2012 Share Posted July 12, 2012 How can I unlink the images associated with the id's in the database? At the moment I use the following delete.php to delete multiple rows: include_once'inc/_dbconnect.php'; $table = mysql_real_escape_string( $_POST["table"] ); $id = $_POST['id']; $delete_id = implode(",", $id); $sql="DELETE FROM $table WHERE id IN ($delete_id)"; $result = mysql_query( $sql ) or die( mysql_error() ); // header("Location: index.php"); In the database it also stores the image location and the file name, so before the row is deleted I'd like to somehow pull the associated image_file name and the location then create a loop (I presume that's what I'd use) to unlink the images, but I'm not sure how to do it. Could someone explain how I might do it? Many thanks in advance, Oraya Quote Link to comment https://forums.phpfreaks.com/topic/265592-how-can-i-unlink-the-images-connected-to-the-id/ Share on other sites More sharing options...
Psycho Posted July 13, 2012 Share Posted July 13, 2012 Well, you have the general idea. What have you written or tried? This sounds like homework. I can't believe you are to the point of writing DELETE queries and don't know how to write a SELECT query. So, write the select query to get the image location. Then create a loop to iterate through all the records. In the loop use unlink() to delete the files. Give it a try and let us know where you are having problems. Quote Link to comment https://forums.phpfreaks.com/topic/265592-how-can-i-unlink-the-images-connected-to-the-id/#findComment-1361179 Share on other sites More sharing options...
oraya Posted July 13, 2012 Author Share Posted July 13, 2012 I know how to write a select query I'm just not sure how to go about deleting multiple image files. Sorry maybe I didn't explain myself very well. Oraya PS I know how to unlink() one file, but I'm just not sure how to delete x amount. Quote Link to comment https://forums.phpfreaks.com/topic/265592-how-can-i-unlink-the-images-connected-to-the-id/#findComment-1361184 Share on other sites More sharing options...
oraya Posted July 13, 2012 Author Share Posted July 13, 2012 Ok this appeared to work but am I doing it correctly or is there an easier way of doing this? Also is there anything I should be adding for security? Sorry so many questions I know, it's just because I'm new and want to make sure I'm doing it correctly. Oraya include_once'inc/_config.php'; include_once'inc/_dbconnect.php'; $table = mysql_real_escape_string( $_POST["table"] ); $image_file = mysql_real_escape_string( $_POST["image_file"] ); $location = mysql_real_escape_string( $_POST["location"] ); $id = $_POST['id']; $delete_id = implode(",", $id); $query="SELECT image_file FROM $table WHERE id IN($delete_id)"; $result=mysql_query($query); $num=mysql_num_rows($result); $i=0; while ($i < $num) { $image_file=mysql_result($result,$i,"image_file"); unlink($base_address . $location . $image_file); $i++; } $sql="DELETE FROM $table WHERE id IN ($delete_id)"; $result = mysql_query( $sql ) or die( mysql_error() ); // header("Location: index.php"); Quote Link to comment https://forums.phpfreaks.com/topic/265592-how-can-i-unlink-the-images-connected-to-the-id/#findComment-1361185 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.