Add addition where clause in MySQL PHP

137 Views Asked by At

I have the following clause

$curSong = $wpdb->query("DELETE FROM ".$wpdb->prefix . "current_requests WHERE id='".$_POST['remove']."'");

and id like to add where page_id = $postid to the WHERE clause

simple I know but its just the syntax I am a little unsure of

Thanks

Henry

4

There are 4 best solutions below

0
On

You mean this?

$query = sprintf("DELETE FROM %scurrent_requests WHERE id='%s' AND page_id='%s'", $wpdb->prefix, $_POST['remove'], $postid);
$curSong = $wpdb->query($query);

You can add more conditions to the where clause using "AND" operator.

2
On
$curSong = $wpdb->query("DELETE FROM ".$wpdb->prefix . "current_requests WHERE id='".$_POST['remove']."' AND page_id='$postid'");
0
On
you can write sql command like
Delete FROM Table where id=123 AND page_id=789 AND route_id=567

as much as you want, you can add more conditions using 'AND' logical       
operator.

if it helps you mark it as answered.

0
On

Modify your code...

Validate the user value to prevent SQL injection

Use htmlspecialchars() or htmlentities() by

$remove= htmlspecialchars($_POST['remove']);
$curSong = $wpdb->query("DELETE FROM ".$wpdb->prefix . "`current_requests` WHERE `id`='$remove' and `page_id` = '$postid'");

Note: You can add many condition by using and or Operator...