You either have no rows or have a masked error which is ending the script prematurely.
SQL result:
$wpdb->get_results()
returns NULL
if there are no results available for debugging use var_dump($myrows)
and to avoid passing a NULL
to the foreach
(which expects an array) use
if ($myrows !== NULL)
{
/* foreach in here */
}
Or
if (!empty($myrows))
{
/* foreach in here */
}
Error reporting:
To check for errors you should add the following at the beginning of your script:
error_reporting(E_ALL);
ini_set('display_errors', true);
Best approach:
if(isset($_POST["delete"]))
{
$checkbox = $_POST['checkbox'];
/* this converts every item to an Integer */
/* which helps sanitize input, and avoid SQL injection attacs */
$checkbox = array_map('intval', $checkbox);
/* select all the rows which will be deleted (before deleting them) */
$myrows = $wpdb->get_results("
SELECT `image_path`
FROM `gate_options`
WHERE `id` IN ( " . implode(', ', $checkbox) . " )
");
/* delete all the rows in one single query */
$wpdb->query("
DELETE
FROM `gate_options`
WHERE `id` IN ( " . implode(', ', $checkbox) . " )
");
/* remove attachments for the deleted rows */
$root = realpath($_SERVER['DOCUMENT_ROOT']);
foreach($myrows as $row)
{
print "$root/wp-content/themes/weld-wide/images/gates/" . $row->image_path;
unlink("$root/wp-content/themes/weld-wide/images/gates/" . $row->image_path);
}
}