0

I am trying to save multiple queries into a database on two different tables. Below is the code that I have tried to no avail. firsttable.a is the same as secondtable.id, con holds the connection info, and everything saves perfectly with one query. Is there something I am missing here?

if(empty($id)){
$uuid = uniqid();
$query = "INSERT INTO firsttable (`id`, `a`, `b`, `uuid`) VALUES (NULL, '$a', '$b', '$uuid')";

$query2 = "INSERT INTO secondtable (`id`, `c`, `d`, `uuid`) VALUES (NULL, '$c', '$d', '$uuid')";
}else{
$query = "UPDATE `firsttable` SET `id` = '$id', `a` = '$a', `b` = '$b', `uuid` = '$uuid' WHERE `id` = $id";

$query2 = "Update INTO secondtable SET `id` = '$a', `c` = '$c', `d` = '$d',

if(!mysqli_multi_query($this->_con, $query;$query2)){
                    throw new Exception(  mysqli_error($this->_con) );

1 Answer 1

1

mysql_multi_query takes two arguments: the database connection, and a single string.

You need to concatenate your two queries together as a string:

mysqli_multi_query($this->con, $query1 . ';' . $query2);

or what you were probably trying to do:

mysqli_multi_query($this->con, "$query1;$query2");

From the php documentation on how to retrieve the result sets for the subsequent queries:

To retrieve the resultset from the first query you can use mysqli_use_result() or mysqli_store_result(). All subsequent query results can be processed using mysqli_more_results() and mysqli_next_result().

The first example shows how it all works.

In your example, though, the correct syntax is UPDATE tablename ..., not UPDATE INTO tablename ....

Sign up to request clarification or add additional context in comments.

3 Comments

w3schools screwed me, they said to simply seperate with a semicolon, not concatenate with the semicolon seperated. Now it does not kill the page, but still does not save to the database - is there a way to output an error saying why the 2nd query update does not work? Also, I never want to insert into table 2, only update existing data, can I change INSERT for table 2 into just update and have it still work?
@Steven Updated the answer, which should be what you need. And if you want to only update existing data, and never add anything new, then yes, changing your insert to a valid update query should still work.
Thanks a lot! Guess I need to use the documentation more, instead of a third party website ;D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.