Jump to content

Recommended Posts

I have googled examples of the use of the mysqli_multi_query and I can't really find anything that applies to what I am doing. Essentially I want to do a insert query, a select query and then a insert query that are dependent on one another but I can't find any examples of anything like this. Does anyone know how to use the mysqli_multi_query() function well and can explain how I can use this??

 

Thanks alot!

Link to comment
https://forums.phpfreaks.com/topic/245323-mysqli_multi_query-function/
Share on other sites

i did something like the following and i'm receiving a syntax error:

 

$q = "INSERT INTO blah(username, password) VALUES('$username', '$password', 1)";

$q .= "SELECT user_id FROM blah WHERE login_name='$login_name'";

$q .= "INSERT INTO blah2(first_name, last_name, user_id) VALUES('$first_name', '$last_name', '$user_id')";

$r = mysqli_multi_query($db, $q);

if($r) {

 

but like i said i'm receiving a syntax error. Any ideas?

your queries need semi colons at the end of them, notice my code. Also your first query has more values than columns

 

$q = "INSERT INTO blah(username, password) VALUES('$username', '$password', 1);";

$q .= "SELECT user_id FROM blah WHERE login_name='$login_name';";

$q .= "INSERT INTO blah2(first_name, last_name, user_id) VALUES('$first_name', '$last_name', '$user_id');";

$r = mysqli_multi_query($db, $q);

Thanks alot that fixed the syntax error that I was receiving but the only problem is the second insert statement is not working and the reason I did the select statement was to get the user_id because the user_id is a foreign key so it has to be the same in both tables.

Save it to a mysql variable like this:

 

$q = "INSERT INTO blah(username, password) VALUES('$username', '$password', 1);";

$q .= "set @user_id = (SELECT user_id FROM blah WHERE login_name='$login_name' limit 1);";

$q .= "INSERT INTO blah2(first_name, last_name, user_id) VALUES('$first_name', '$last_name', @user_id);";

$r = mysqli_multi_query($db, $q);

Guy,

 

You are amazing!! I set the select to a variable just like you said and it worked perfectly. I understand mysql okay, but adding php to it is different because you have to figure out how to use certain functions with php. Is there somewhere I can go to find out how to use mysql in php?

 

Also with the insert last id query, where would I use that? Where I manage my database or in my php?

 

Once again thanks so much for your help I've been struggling with this problem for a while.

You would/could use it like this:

$q = "INSERT INTO blah(username, password) VALUES('$username', '$password', 1);";

$q .= "INSERT INTO blah2(first_name, last_name, user_id) VALUES('$first_name', '$last_name', last_insert_id());";

$r = mysqli_multi_query($db, $q);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.