I have the following code basically I am deleting one or more rows from a database selected using checkboxes and also then deleting the associated images from the server using PHP's unlink() function:
if(isset($_POST["delete"])){
$checkbox = $_POST['checkbox'];
foreach($checkbox as $id) {
print "here 1";
$wpdb->query("DELETE FROM `gate_options` WHERE `id` = $id LIMIT 1");
print "here 2 after delete row in db";
}
$myrows = $wpdb->get_results("SELECT `image_path` FROM `gate_options` WHERE `id` = $id LIMIT 1");
print "here 3 after assigining myrows to query";
$root = realpath($_SERVER['DOCUMENT_ROOT']);
print "here 4 after assigning root path";
foreach($myrows as $row) {
print "in the loop";
print "$root/wp-content/themes/weld-wide/images/gates/" . $row->image_path;
unlink("$root/wp-content/themes/weld-wide/images/gates/" . $row->image_path);
}
}
The code line print "in the loop"; that is inside the very last foreach isn't printing to screen hence it is not going inside the loop, question is why?
Thanks in advance
You are trying to select rows you just have deleted, this can't work.
$root = realpath($_SERVER['DOCUMENT_ROOT']);
foreach($checkbox as $id) {
$id = intval($id); //protects from sql injection (assuming your ids are integers)
//First get the row to delete its image :
$myrows = $wpdb->get_results("SELECT `image_path` FROM `gate_options` WHERE `id` = $id LIMIT 1");
print "Removing : $root/wp-content/themes/weld-wide/images/gates/" . $myrows[0]->image_path;
unlink("$root/wp-content/themes/weld-wide/images/gates/" . $myrows[0]->image_path);
//Then delete the row :
$wpdb->query("DELETE FROM `gate_options` WHERE `id` = '$id' LIMIT 1");
}