son Posted October 18, 2011 Share Posted October 18, 2011 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 More sharing options...
thescientist Posted October 18, 2011 Share Posted October 18, 2011 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 More sharing options...
justsomeguy Posted October 18, 2011 Share Posted October 18, 2011 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 More sharing options...
Don E Posted October 19, 2011 Share Posted October 19, 2011 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 More sharing options...
justsomeguy Posted October 19, 2011 Share Posted October 19, 2011 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 More sharing options...
son Posted October 19, 2011 Author Share Posted October 19, 2011 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 More sharing options...
justsomeguy Posted October 19, 2011 Share Posted October 19, 2011 Something like this may work: $success = true; foreach ($queries as $q){ $success &= mysql_query($q);} if ($success){ echo 'all queries successful';}else{ echo 'at least one query failed';} Link to comment Share on other sites More sharing options...
son Posted October 20, 2011 Author Share Posted October 20, 2011 Thanks. Will give this a go... Son Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.