简体   繁体   中英

Inserting an ID from one table into another when a new record is inserted

I have a form, which contains an employees personal information. I update the form, and it inserts into a table, called employee_info . It updates the table with the details and inserts a NEW ID( employee_id ). In my database, I have another table called department_info , and the field which is relevant is department_id . This is the php markup, for inserting data into the database:

$sql_data_array = array('employee_firstname' => $employee_firstname); //other variables go here

if (ACCOUNT_DOB == 'true') $sql_data_array['driver_dob'] = tep_date_raw($driver_dob);

tep_db_perform(TABLE_EMPLOYEES, $sql_data_array);

$employee_id = tep_db_insert_id();

What I need to do is, when the form is updated, and data is inserted into the employee_info table, I need it to insert a new id for employee_id (which is already happening), and also to insert the department_id into the employee_info table.

The department_id is used to login, and I want to show a list of the employees, which belong to the department. Can anyone tell me how I would do this?

When you insert with PHP, most of the implementations return so called last insert id. This is a value of AUTO_INCREMENT column.

Even if your implementation does not support it, there should be a function that returns it.

EDIT: From your update, this seems to be osCommerce. The problem with your code is that method tep_db_perform is used to insert one or MORE elements. What will happen, if you insert 2 rows? But in simple case, use tep_db_insert_id(), it should return last id.

php function mysqli_insert_id retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT). Get this id and use it in other tables.

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