doutuo1908 2017-06-21 14:17
浏览 36
已采纳

将数组值插入数据库时​​出错[重复]

This question already has an answer here:

I am inserting multiple images into database but getting such error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0, 1) VALUES (Array, Array)' at line 1

INSERT INTO CompleteProjectImage (0, 1) VALUES (Array, Array)

I am holding list of selected image in the controller and passed it to my model.

Here is my model coding to insert into database.

public function create($projectDetails, $projectDescriptions, $projectImages){

    $this->db->trans_begin();


    $this->db->insert(self::$tblCompleteProject, $projectDetails);
    $insert_id = $this->db->insert_id();

    foreach ($projectDescriptions as $projectDescription) {
        $insertDescription = array(
            'project_id'   => $insert_id,
            'description'  => $projectDescription,
        );
    }

    $this->db->insert(self::$tblCompleteProjectDescription, $insertDescription);

    foreach ($projectImages as $projectImage) {
        $insertImage[] = array(
            'project_id'   => $insert_id,
            'img_src'      => $projectImage['img_src'],
        );
    }

    $this->db->insert(self::$tblCompleteProjectImage, $insertImage);  // Error inserting to this database

    if ($this->db->trans_status() === FALSE)
    {
        $this->db->trans_rollback();
    }
    else
    {
        $this->db->trans_commit();
        return ($this->db->affected_rows() != 1) ? false : true;
    }

} 

print_array($insertImage); resulting :

Array
(
    [0] => Array
        (
            [project_id] => 5
            [img_src] => 594a7f9a79285.jpg
        )

    [1] => Array
        (
            [project_id] => 5
            [img_src] => 594a7f9a792851.jpg
        )

)
</div>
  • 写回答

1条回答 默认 最新

  • dongmo2324 2017-06-21 14:20
    关注

    You can not insert an array into database you should either turn it to a Json object or select indexes one by one.

    insertImage is an array but your Insert func doesn't accept an array and directly insert the parameter in the query !

    A turnaround for you:

    foreach ($projectImages as $key => $projectImage) {
            $insertImage[$key] = array(
                'project_id'   => $insert_id,
                'img_src'      => $projectImage['img_src'],
            );
    
    $this->db->insert(self::$tblCompleteProjectImage, $insertImage[$key]);
    }
    

    I am not sure this is what you want but according to the snippet this is the proper way of inserting

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。
  • ¥15 SQL Server analysis services 服务安装失败