Jump to content

[SOLVED] syntax problem


Minase

Recommended Posts

dont know the best title but here is what i must do but cant

 

foreach($_POST as $a => $b){
	if(preg_match("/x/",$a) && $b == 'y'){

		$id = str_replace("x","",$a);
		$note_query = $db->query("DELETE FROM `" . DBPREFIX . "messages` WHERE `ID` != '".$id."' AND `ReciverID` = '".$_SESSION['user_id']."' ");

	}
}

 

the problem is that it delete all mesages.

it is normal cause it execute multiple querys with diferent ID like

if 2 messages and 2 selected for not deleting

DELETE FROM messages WHERE ID != 1;

DELETE FROM messages WHERE ID != 2;

it will automatically delete both of them,dont have an ideea how to do it so it will work normally.

thanks

Link to comment
https://forums.phpfreaks.com/topic/120376-solved-syntax-problem/
Share on other sites

 

You need to do it all in one query. This example will nor work:

DELETE FROM messages WHERE ID != 1;
DELETE FROM messages WHERE ID != 2;

The first query deletes all (including id 2) except id 1 then the second deletes id 1.

 

I think you can modify your code to something like this:

<?php
foreach($_POST as $a => $b){
	if(preg_match("/x/",$a) && $b == 'y'){
		$ids[] = str_replace("x","",$a);
	}
}
        $id = join(",", $ids);
$note_query = $db->query("DELETE FROM `" . DBPREFIX . "messages` WHERE `ID` NOT IN (".$id.") AND `ReciverID` = '".$_SESSION['user_id']."' ");
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.