MySQL optionally allows having multiple statements in one statement string. Sending multiple statements at once reduces client-server round trips but requires special handling.
Multiple statements or multi queries must be executed with mysqli_multi_query().
‘not working’ is an inappropriate description of your actual state. provide code, working examples, error messages. at least you should stop overwriting $sql and start concatenating like the examples in the manuals do.
I would use prepared statements and not write less secure code.
And I would use different names for the variables that were meant to have different values.
If it were only one or two queries I would probably write separate blocks of code for each.
But if there were more and they all had a lot in common I would wrap the code inside a function and pass what ever was different to the function as parameters. eg.
function run_my_query($input_array, $query) {
$query_string = $query;
.....
foreach($input_array as $key => $value) {
.....
run_my_query($data1, $query1);
run_my_query($data2, $query2);
It’s just your regular function to run a query is meant here. Call this function as many times as many queries you have to run.
Speaking of best practices, you definitely have to use prepared statements. In this regard I would suggest PDO over mysqli. Just because PDO is more consistent when working with prepared statements and has some nice features that make your code way more clean.
As @Mittineague said above, the issue is that you create the first query, but then you don’t execute it. You then create a second query, but because you’ve used the same variable name $sql, you’ve overwritten the first query now. So when you finally come to actually execute the query, it’s only the second one.
If this is related to the other thread you had (sorry, I can never remember poster names) where we talked about splitting the checkboxes out into a separate table {
you have missed out a bit. There seems to be no way to link your row in tbl_project2 to the row in tbl_project2b. You also seem to be writing the value of one of the checkboxes into the first table, and the value of the second into the second, which isn’t what I meant.
What I meant was, store the user details (name, description, gender etc.) in a single users table, and have a unique id for each user. Once you’ve inserted that row into the users table, retrieve the unique user id. Then, if the form-filler ticked either bike or cycle, insert an individual row for each of those values into the second table, along with the user id, to link them together.
Out of interest, which specific part of that post provided the answer? I didn’t immediately spot anything on that page that isn’t mentioned above, other than the OPs code in that question used the old-style database calls.
You are still not using mysqli_multi_query properly. This function’s purpose is different and it shouldn’t be used by a whim. So you will have problems in the future.
Like you have been told, to run two queries, you need two separate calls, exactly like it is listed in the solution which you “aren’t gonna using”. It is always a pleasure to see a learner who is doing everything their own way, despite what they are being told, ending up in a trouble. Good luck.
If you configure mysqli to report errors by itself, you will need no manual error checking. So if you will add the following line before mysqli_connect,