Question

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

Was it helpful?

Solution

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

}

OTHER TIPS

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

    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top