简体   繁体   中英

Using LAST_INSERT_ID() to insert newest id from one table to another

I know there are many questions about this topic. I read almost all of them, but I keep getting MySQL syntax errors. I'm using my WAMP server atm. I checked my MySQL version to be 5.7.9, so this should be ok.

I have this query:

INSERT INTO users (profile_pic, first_name, last_name, 
address, zip, city, phone, age, job, education, registration_date, email, pass) 
VALUES ('$final_profile_pic_file', '$first_name', '$last_name', '$address', '$zip', '$city', 
'$phone', '$age', '$job', '$education', NOW(), '$email', SHA1('$pass') ); 

INSERT INTO tickets (user_id, ticket_date) 
VALUES (LAST_INSERT_ID(), NOW());

The first INSERT users works fine. I then want to put in the just created user id from the users table into the tickets table right away with LAST_INSERT_ID() . I also tried to put it as a variable and then use that variable as a value.

What am I doing wrong?

I needed to use @mysqli_multi_query instead of @mysqli_query . That solved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM