所以我对以下内容感到有些困惑。我有两个表,项目和变化。
项目:
- id
- title
- description
- datecreated
变化:
- id
- title
- description
- projectid FOREIGN KEY
- datecreated
我无法弄清楚如何
insert into change (name, description, projectid) value (:name, :description, :projectid)
select id from project
where name = $name
重要 - 插入中的名称和描述由php变量使用表单提供。
重要 - 必须使用PDO
实际代码:
$sql = "INSERT INTO change (title, description, project_id) SELECT :title, :description, id FROM project WHERE title = :project_title";
$query = $db->prepare($sql);
$query->execute(array(":title" => $title, ":description" => $description, ":project_title" => $created));
这就是我最后所做的事情,但Barmar在这个和另一个问题上得到了帮助。
$sql = "INSERT INTO `change` (`title`, `description`, `project_id`) SELECT :title, :description, id FROM project WHERE title = :project_title";
$query = $db->prepare($sql);
$query->execute(array(":title" => $title, ":description" => $description, ":project_title" => $created));
答案 0 :(得分:1)
插入时,您可以使用values
子句,也可以使用select
指定数据源。你不能同时使用它们。
你想:
INSERT INTO `change` (`title`, `description`, `projectid`)
SELECT :title, :description, id
FROM project
WHERE title = :project_title