我有一张表格,其中包含员工的个人信息。我更新了表单,并将其插入到名为 employee_info
的表中。它使用详细信息更新表并插入新 ID (employee_id
)。在我的数据库中,我有另一个名为 department_info
的表,相关字段是 department_id
。这是 php 标记,用于将数据插入数据库:
$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();
我需要做的是,当更新表单并将数据插入employee_info
表时,我需要它为employee_id
插入一个新的ID(这已经发生了),并将 department_id
插入 employee_info
表中。
department_id
用于登录,我想显示属于该部门的员工列表。谁能告诉我该怎么做?
最佳答案
当您使用 PHP 插入时,大多数实现都会返回所谓的最后插入 ID。这是 AUTO_INCREMENT 列的值。
即使您的实现不支持它,也应该有一个函数返回它。
编辑: 从您的更新来看,这似乎是 osCommerce。您的代码的问题在于方法 tep_db_perform 用于插入一个或多个元素。如果插入 2 行会发生什么?但在简单的情况下,使用tep_db_insert_id(),它应该返回最后一个id。
关于php - 插入新记录时将一个表中的 ID 插入到另一个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15115619/