简体   繁体   中英

Is it possible to use INSERT INTO and then SELECT later?

I'm trying to insert values into a database, but one of the values is a primary key from another table. I have two tables, address and User. User has the primary key of address as a foreign key. I am trying to insert values the customer inserts into the database, but User is not getting the addressid(Primary Key of address). The main purpose of this is so I can have address as a separate table and pull the address when needed to the User table. I am using dreamweaver and phpmyadmin to do this.

I was thinking maybe do:

$insertSQL = "INSERT INTO User (name, email, addressid) VALUES ('%".GetSQLValueString($_POST['Name'], "text"). "%', '%".GetSQLValueString($_POST['Email'], "text")."%' SELECT addressid FROM address";

But this is giving me an error of

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't'%', '%'t'%' SELECT addressid FROM address' at line 1

If someone could please help, that would be great. Sorry I have never been taught SQL nor PHP so I don't know much code.

You gotta do 2 queries. If you use an incremental primary, you can catch the last inserted id to build your SELECT query.

Example: http://php.net/mysql_insert_id

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