Home > Software engineering >  WordPress: $wpdb not deleting from SQL unless put in manually, manually ('2') works with d
WordPress: $wpdb not deleting from SQL unless put in manually, manually ('2') works with d

Time:11-09

I have this:

$value = $_POST["check"][0];
$toDELETE = $_POST["itemDELETE" . $value];
$removeFROM = $wpdb->prefix.'plugin_items';
try {
    $wpdb->query($wpdb->prepare("DELETE FROM $removeFROM WHERE itemID = ''.$toDELETE .''"));
    return true;
} 

It works when I manually put in the ID like this:

$value = $_POST["check"][0];
$toDELETE = $_POST["itemDELETE" . $value];
$removeFROM = $wpdb->prefix.'plugin_items';
try {
    $wpdb->query($wpdb->prepare("DELETE FROM $removeFROM WHERE itemID = '2'"));
    return true;
}  

I can't seem to figure out what exactly I've done wrong. I've also tried itemID = $toDELETE"

This is the input:

<input type='checkbox' name='check[]' value='$index' onChange='this.form.submit()' checked='checked'>
<input type='hidden' name='itemDELETE$index' value='$bookID'>

When I use var_export($_POST) it shows this array:

array ( ‘itemDELETE0’ => ‘2’, )

CodePudding user response:

You should use placeholder for itemID.

try out this code.

 $wpdb->query($wpdb->prepare("DELETE FROM $removeFROM WHERE itemID = %d", $toDELETE));

Reference :- https://developer.wordpress.org/reference/classes/wpdb/prepare/

  • Related