首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在预置语句php中不同行插入多条记录

如何在预置语句php中不同行插入多条记录
EN

Stack Overflow用户
提问于 2018-07-04 23:12:26
回答 1查看 35关注 0票数 1

我在我的数据库中插入了一些带有值的字符串,让我们称之为table_1,现在我有了数组,我想将它们插入到我的SQL数据库的table_2中的单独行中。

代码语言:javascript
复制
$sql = 'INSERT INTO ' . $table_1 . '(shipping_fee, waybill_status, pickup_fee, ) 
        VALUES(:shipping_fee, :waybill_status, :pickup_fee)';

$stmt = $this->dbConn->prepare($sql);
$stmt->bindParam(':shipping_fee', $s_shipping_fee);
$stmt->bindParam(':waybill_status', $s_waybill_status);
$stmt->bindParam(':pickup_fee', $this->pickup_fee);

if($stmt->execute()){ //THIS INSERTED THE STRINGS PERFECTLY
    //NOW ALL VALUES TO BE INSERT INTO $sqal is an array

    $sqal = 'INSERT INTO ' . $table_2. '(id, waybill_number, client_id, item_name, item_weight, item_length, item_width, item_category, date_added) VALUES(null, :waybill_numberr, :client_idaa, :item_name, :item_weight, :item_length, :item_width, :item_category, :date_added)';

    $stmtaaa = $this->dbConn->prepare($sqal);
    $stmtaaa->bindParam(':item_name', $this->item_name); //ARRAY
    $stmtaaa->bindParam(':item_weight', $this->item_weight); //ARRAY
    $stmtaaa->bindParam(':item_length', $this->item_length); //ARRAY
    $stmtaaa->bindParam(':item_width', $this->item_width); //ARRAY
    $stmtaaa->bindParam(':item_category', $this->item_category); //ARRAY

    $stmtaaa->execute(); //HoW do I go about this.
} else {
    echo "Could not insert";
    exit();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-04 23:38:18

您在第一个查询中有一个语法错误,列或值列表中不应该有尾随的逗号,

您可以通过使用不同的值多次执行prepare来插入数组。这个例子假设你所有的数组都是由数字索引的(从零开始)。

上面的代码示例还绑定了比它绑定的更多的列,因此您需要将一个值绑定到每一列。client_idaadate_added缺少绑定(我只是添加了一些随机的占位符)。

代码语言:javascript
复制
$sql = "INSERT INTO $table_1 (shipping_fee, waybill_status, pickup_fee) 
        VALUES (:shipping_fee, :waybill_status, :pickup_fee)";

$stmt = $this->dbConn->prepare($sql);
$stmt->bindParam(':shipping_fee', $s_shipping_fee);
$stmt->bindParam(':waybill_status', $s_waybill_status);
$stmt->bindParam(':pickup_fee', $this->pickup_fee);

if ($stmt->execute()) {
    $sqal = "INSERT INTO $table_2 (id, waybill_number, client_id, item_name, item_weight, item_length, item_width, item_category, date_added) 
             VALUES (null, :waybill_numberr, :client_idaa, :item_name, :item_weight, :item_length, :item_width, :item_category, :date_added)";

    $stmtaaa = $this->dbConn->prepare($sqal);

    foreach ($this->item_weight as $key => $value) {
        $stmtaaa->execute(["waybill_numberr" => '1',   // Change this to your actual value
                           "client_idaa" => '1',       // Change this to your actual value
                           "item_name" => $value, 
                           "item_weight" => $this->item_weight[$key],
                           "item_length" => $this->item_length[$key],
                           "item_width" => $this->item_width[$key],
                           "item_category" => $this->item_category[$key],
                           "date_added" => '1']);
    }
} else {
    echo "Could not insert";
    exit();
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51176754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档