OK, so your code can’t work, because you use $wpdb->delete
incorrectly.
Let’s take a look at Codex. $wpdb->delete
takes 3 params (one is optional):
- $table (string) (required) Table name.
Default: None - $where (array) (required) A named array of WHERE clauses (in column -> value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be ‘raw’.
Default: None - $where_format(string/array) (optional) An array of formats to be mapped to each of the values in $where. If a string, that format will be used for all of the items in $where. A format is one of ‘%d’, ‘%f’, ‘%s’ (integer, float, string; see below for more information). If omitted, all values in $where will be treated as strings unless otherwise specified in wpdb::$field_types.
Default: null
And it returns:
It returns the number of rows updated, or false on error.
So what’s wrong with your code?
$id = $print->id;
$del = $wpdb->delete( 'bookings', $id );
You pass $id
as second param, but it’s just a number, and you should pass an array of where clauses…
So how should it look?
foreach ( $result as $print ) {
$del = $wpdb->delete( 'bookings', array( 'id' => $print->id ) );
if ( $del ) {
echo "<script>
alert('record deleted');
</script>";
}
}