dongzhang1864 2013-05-20 19:28
浏览 27

使用implode [duplicate]在sqlite中插入多行

This question already has an answer here:

I have tried to insert multiple rows but got empty rows in database and other errors. So,tried to use implode (as a better alternative?) but must be doing something wrong here.

$sql = array(); 
    foreach($_POST as $key => $value ) {
        $sql[] = '("'.sqlite_escape_string($key['cust_name']).'", '.$key['address'].')';
    }

    $stmt = $db->prepare('INSERT INTO customers VALUES (cust_name, address) '.implode(','. $sql));
    $stmt->execute($sql); 
</div>
  • 写回答

1条回答 默认 最新

  • dongzuoyue6556 2013-05-20 19:49
    关注

    Would it be better to do something like the following?

    <?php
    
    $into = "";
    $values = "";
    
    $cnt = 0;
    $post_cnt = count($_POST);
    foreach($_POST as $key => $value) {
        $into .= sqlite_escape_string($key).($cnt < $post_cnt && $post_cnt > 1 ? ", ": "");
        $values .= "'".sqlite_escape_string($value)."'".($cnt < $post_cnt && $post_cnt > 1 ? ", ": "");
        $cnt++;
    }
    $sql = "INSERT INTO customers (".$into.") VALUES (".$values.");";
    
    $stmt = $db->prepare($sql);
    $stmt->execute();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程