Php 使用PDO的多个插入
我有数据库表:Php 使用PDO的多个插入,php,mysql,pdo,Php,Mysql,Pdo,我有数据库表:图像和类别 当前,在类别表中插入的唯一函数类似于: public function add($ttitle) { try { $stmt = $this->db->prepare("INSERT INTO category (title) VALUES(:title)"); $stmt->bindparam(":title",$ttitle); $stmt->
图像
和类别
当前,在类别表中插入的唯一函数类似于:
public function add($ttitle)
{
try
{
$stmt = $this->db->prepare("INSERT INTO category (title) VALUES(:title)");
$stmt->bindparam(":title",$ttitle);
$stmt->execute();
return true;
}
catch(PDOException $e)
{
echo $e->getMessage();
return false;
}
}
我有一个表格,输入标题和插入图片的URL的可能性
单击submit the title I want to the category table,至此ok,图像应该转到表images,每个图像都有一个id,但与类别的id有内部连接
表格图像
:
id_image | category_id | dir_image
我不能正确地做这件事
$stmt = $this->db->prepare("INSERT INTO category (title) VALUES(:ttitle)");
$stmt = $this->db->prepare("SELECT id_image FROM images WHERE id_category = category_id ");
我想要的结果示例:
html表单
Category title: Test
Image1: image1.png
Image2: image2.png
Image3: image3.png
Image4: image4.png
Submit
提交后
表格类别:
id_category | title
1 Test
表格图像:
id_image | category_id | dir_image
1 1 image1.png
2 1 image2.png
3 1 image3.png
4 1 image4.png
更新:
public function add($ttitle,$images)
{
try {
$stmt = $this->db->prepare("INSERT INTO category (title) VALUES(:title)");
$stmt->bindparam(":title",$ttitle);
$stmt->execute();
$lastId = $db->lastInsertId();
$imgs = count($images);
for ($i = 0; $i < $imgs; $i++){
$stmt = $this->db->prepare("INSERT INTO images (category_id, dir_image) VALUES (:title)");
$stmt->bindparam(":category_id",$lastId);
$stmt->bindparam(":dir_image",$images);
$stmt = $this->db->prepare($sql);
$stmt->execute()
}
return true;
}
catch(PDOException $e) {
echo $e->getMessage();
return false;
}
}
公共功能添加($ttitle,$images)
{
试一试{
$stmt=$this->db->prepare(“插入类别(标题)值(:标题)”);
$stmt->bindparam($title“,$ttitle);
$stmt->execute();
$lastId=$db->lastInsertId();
$imgs=计数($images);
对于($i=0;$i<$imgs;$i++){
$stmt=$this->db->prepare(“插入到图像(类别id,目录图像)值(:title)”);
$stmt->bindparam(“:category_id”,$lastId);
$stmt->bindparam(“:dir_image”,$images);
$stmt=$this->db->prepare($sql);
$stmt->execute()
}
返回true;
}
捕获(PDO$e){
echo$e->getMessage();
返回false;
}
}
有几件事:
for
循环中的第二条prepare语句VALUES()
中为$images
数组编制索引,或使用foreach
:
$stmt = $this->db->prepare("INSERT INTO images (category_id, dir_image)
VALUES (:category_id, :dir_image)");
$stmt->bindParam(":category_id" ,$lastId);
$stmt->bindParam(":dir_image", $image);
for ($i = 0; $i < count($images); $i++){
$image = $images[$i];
$stmt->execute();
}
你一直在说内部连接,我想你指的是外键。看看PDO的lastInsertId()
方法,它返回插入category
时分配的ID。然后,当插入images
时,您可以使用该ID作为category\u ID
。谢谢@Barmar我在我的问题中插入了详细信息,请see@YourCommonSense我犯了一个错误?谢谢@parfait,meIt的bindParam
,而不是bindParam
@Gisele,因为PHP是区分大小写的。谢谢,我会更正,但目前没有显示错误。我认为,如果有任何错误,或者更具体地说,区分大小写的错误将显示错误消息,即使激活以显示错误,如果我通知bindparam,它们也不会出现
$stmt = $this->db->prepare("INSERT INTO images (category_id, dir_image)
VALUES (:category_id, :dir_image)");
$stmt->bindParam(":category_id", $lastId);
$stmt->bindParam(":dir_image", $item);
foreach ($images as $item){
$stmt->execute();
}