带 PDO 的多个刀片

Multiple inserts with PDO(带 PDO 的多个刀片)
本文介绍了带 PDO 的多个刀片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有数据库表:imagescategory

目前唯一插入到类别表的函数是这样的:

Currently the only function to insert in category table is similar to this:

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;
    }

}

我有一个表格可以输入标题和插入网址图片的可能性.

I have a form to enter title and the possibility of inserting urls images.

通过单击提交标题,我想转到类别表,到目前为止还可以,图像应该转到表图像,每个图像都有一个 id,但内部连接到类别的 id.

By clicking on the submit the title I want to go to the category table, so far ok, images should go to the table images, an id for each image but with inner join to the id of the category.

表格images:

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;
    }

}

推荐答案

几件事:

  1. 删除for循环中的第二个prepare语句
  2. 在sql语句的VALUES()中添加绑定参数
  3. 使用 for 循环迭代器或使用 foreach
  4. 索引 $images 数组
  1. Remove the second prepare statement inside for loop
  2. Add binded params into the VALUES() of sql statement
  3. Index the $images array with for loop iterator or use foreach

查看调整后的for循环:

$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();
} 

或者使用 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", $item);
foreach ($images as $item){
    $stmt->execute();
} 

这篇关于带 PDO 的多个刀片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)