【问题标题】:codeigniter insert many images name into databasecodeigniter 将许多图像名称插入数据库
【发布时间】:2015-05-30 03:02:05
【问题描述】:

我正在构建上传器图像并将其存储到数据库中,我已经可以将许多图像上传到文件夹,但我无法插入所有上传的图像名称,我不知道如何插入数据库,首先我有发生错误时,在下面的代码中提出建议,其次,如果图像计数不同,我不知道将其放入数据库的查询,例如 1-10 个图像,最后一个问题,如果我查询“SELECT id ...”和我想返回它,有没有办法将它返回为字符串或整数?如果我使用 row() 它将返回 stdClass 对象。请帮我, 以下是我的代码:

控制器:

$this->load->library("myupload", "form_validation");
        $this->load->model("testModel");
        $barangImage = array();

        if($this->input->post("formSubmit")) {
            $this->form_validation->set_rules("nama", "Nama", "required|trim");

            if($this->form_validation->run()) {
                $insertData = array(
                    "nama" => $this->input->post("nama")
                );
                if($id = $this->testModel->add($insertData)) {

                    //print_r($id);

                    if(isset($_FILES) && $image = $this->myupload->uploadFile($_FILES)) {
                        //$image here is already fill with all images name

                        if(isset($image["error"]) && $image["error"]) {
                            echo $image["error"];
                        }else {
                            foreach($image as $img) {
                                $barangImage = array(
                                        "gambar" => $img,
                                        "barangid" => $id
                                );

                            }
                            //but when i put into barangImage,
                            //it only stored last image name
                            print_r($barangImage);
                            //output `Array ( [gambar] => 2.JPG [barangid] => Array ( [id] => 52 ) )`
                        }
                    }

                    if($id = $this->testModel->add_images($barangImage)) {
                        echo "SUCCESS !!!";
                    }else {
                        echo "FAIL INSERT IMAGES!!!";
                    }
                }else {
                    echo "FAIL INSERT DATA NAMA";
                }
            }else {
                echo "FAIL VALIDASI RUN";
            }
        }

型号:

public function add($newData){
        $this->db->insert("cobabarang", $newData);

        $nama = $newData["nama"];
        $id = $this->db->query("SELECT id FROM cobabarang WHERE nama = \"$nama\"");

        return $id->row_array();
    }

    public function add_images($newImage) {

        //$this->db->insert("cobagambar", $newImage);

        $id = $newImage["barangid"]["id"];
        $gambar = $newImage["gambar"];

        $this->db->query("INSERT INTO cobagambar(barangid, gambar1) VALUES($id, \"$gambar\")");
    }

【问题讨论】:

    标签: php mysql image codeigniter insert


    【解决方案1】:

    这里有错误:

    foreach($image as $img) 
    {
        $barangImage = array(
             "gambar" => $img,
             "barangid" => $id
        );    
    }
    

    $barangImage 更改为$barangImage[]

    当您将图像放入数据库时​​,我建议使用json_encode($barangImage),然后在您打算使用图像时使用json_decode($images-json-string)

    【讨论】:

      【解决方案2】:

      你的 foreach 循环有问题

        foreach($image as $img) {
            $barangImage = array(
              "gambar" => $img  //might be img['img'] I guess $img is again an array...you hvae to check that 
              "barangid" => $id  //might be $img['id']check on this too..will be $img['id'] I guess
            );
      
         }
      

      我的猜测是$img 又是一个带有一些键的数组。你真的需要检查一下你可以像这样直接在foreach循环本身中调用插入函数,

        foreach($image as $img) {
            $barangImage = array(
              "gambar1" => $img['img'], //I guess $img is again an array...you hvae to check that 
              "barangid" => $img['id'] //check on this too..will be $img['id'] I guess
            );
                $id = $this->testModel->add_images($barangImage));
         }
      

      注意:数组barangImage 中的keys 必须是表中的列名。即

      gambar1 and barangid will be your column names. so you can directly use codeIgniter's active records.
      

      只需更改您的 add_images 函数

         public function add_images($newImage) {
      
          $this->db->insert("cobagambar", $newImage);
      
      }
      

      【讨论】:

        猜你喜欢
        • 2017-07-13
        • 2017-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多