Jump to content

How can I unlink the images connected to the id


oraya
 Share

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

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.

 Share

×
×
  • 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.