Jump to content

Delete From Several Tables


son

Recommended Posts

On click on a button data in db should be either totally removed or all table data other than one deleted. I am having trouble for the right way of running all those queries in one go. I post the code below:

$deleteQuery = "DELETE FROM tab1 WHERE tabID= $tabID;";  $deleteQuery .= "DELETE FROM tab2 WHERE tabID= $tabID;";    if ($_GET['conf'] == '2')   {   $deleteQuery .= "DELETE FROM tab3 WHERE tabID= $tabID;";	  $deleteQuery .= "DELETE FROM tab4 WHERE tabID= $tabID";   }   else   {   $deleteQuery .= "DELETE FROM tab3 WHERE tabID= $tabID";   }if ($deleteResult = mysqli_query ($dbc, $deleteQuery))   {echo "Success"}else{echo "Did not work";}

Where am I going wrong? Son

Link to comment
Share on other sites

I don't think concatenating queries will work. You will need to setup a loop to execute those queries, or just run them consecutively.

Link to comment
Share on other sites

The MySQL extension for PHP does not support running more than one query at a time, for security reasons. That's another one of the protections that the PHP developers added to try and help save people who don't know what they're doing.

Link to comment
Share on other sites

Jsg, You mean the PHP developers added protection against doing what Son is trying to do? Or did you mean protection against trying to run consecutive queries right after another at a time?(what scientist suggested)

Link to comment
Share on other sites

The first one, you can't send more than one query in a query string to mysql_query, you can only send one query at a time. That's a protection against SQL injection attacks which try to append another query onto the one you're trying to run. It's not helpful for people who protect against SQL attacks themselves, it's similar to magic_quotes in that it tries to protect programmers who don't really understand what they're doing.

Link to comment
Share on other sites

I am with you and will run all queries separately... Just as I always use

if ($deleteResult = mysqli_query ($dbc, $deleteQuery))   {echo "Success"}else{echo "Did not work";}

is there a way to check in one go if they all went fine without causing any issues? Or is this not a good idea? In any case, I like to dispay text to show all went ok (all queries)... Son

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...