我正在为学校项目创建一个电影数据库,我试图在sqlite中为多对多关系添加2个值。但是,由于某种原因,第二个查询不返回新添加的电影的主键,但始终是1.我用sql测试了查询,然后他们返回了正常的值,所以我不认为这是错的SQL查询。
public function add_movie($title, $genre, $actor, $director, $agecategory, $warning){
$query1 = $this->db->prepare(
"INSERT INTO movie
(title, agecategorynumber) VALUES (?,?);"
);
$query1->bindValue(1, $title);
$query1->bindValue(2, $agecategory);
try{
$query1->execute();
}catch(PDOException $e){
die($e->getMessage());
}
$query2 = $this->db->prepare(
"SELECT movie.movienumber
FROM movie
WHERE movie.title LIKE ?"
);
$query2->bindValue(1, $title);
try{
$titlenumber = $query2->execute();
}catch(PDOException $e){
die($e->getMessage());
}
$query3 = $this->db->prepare(
"INSERT INTO movieactor
(movienumber, actornumber) VALUES (?,?);"
);
$query3->bindValue(1,$titlenumber);
$query3->bindValue(2,$actor);
try{
$query3->execute();
}catch(PDOException $e){
die($e->getMessage());
}
}