我在网上搜索了很长时间,但没有找到解决方案。我正在尝试在一个表中插入,获取记录的 ID 并在另一个表中插入 x 次。我的代码是
$first = true;
$sql = 'INSERT INTO
`pf_employees` (firstname, surname, `occupationGroup`, birthday, image)
VALUES ("'.$name.'", NULL, NULL, NULL, NULL)';
$sql .= 'INSERT INTO `pf_roster_employees` (weekID, dayID, employeeID) VALUES ';
foreach($days as $temp) {
if(!$first)
$sql .= ', ';
$sql .= '("'.$weekID.'", "'.$temp.'", last_insert_id())';
$first = false;
}
$result = $config['database']->query($sql);
这会导致类似这样的结果:
[...] INSERT INTO pf_roster_employees (weekID, dayID, employeeID) VALUES ("1", "1", last_insert_id()), ("1", "4", last_insert_id()), ("1", "5", last_insert_id())
.
口译员说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 'INSERT INTO
pf_roster_employees(weekID, dayID, employeeID) VALUES ("1", "1", ' at line 1
有没有更方便的方法来复制第二个查询?我的意思是,SQL 比 PHP 快得多,当然,我对使用这种肮脏的代码感到非常惭愧。
谢谢你。