将多个图像插入数据库时​​,在连接到图像表的主表中添加了多行

时间:2019-02-04 08:54:37

标签: php codeigniter mysqli

我正在尝试在Codeigniter中上传多个图像。对于特定产品,我想在使用“ lastid”时将上载的图像添加到另一个表中,称为“ image” ....,以便在图像中获得pid(来自表product_multi的产品ID),在product_multi表中插入多个行。 ..数量行是根据我上传的图片数量而定。
有人帮助我解决了这个问题。如果我不使用传递lastid的变量,它可以正常工作/ ...但是我想在图像表中使用pid:(

这就是我如何获得数据This is how getting

这是我的模特

#include <iostream>
#include <sstream>


int main(void)
{
    int arr[] = { 192,168,1,0 };
    std::stringstream ss;
    ss << arr[0] << "." << arr[1] << "." << arr[2] << "." << arr[3];


    std::cout << ss.str() << std::endl;
}

控制器代码::

<?php


class ProductmModel extends CI_Model
{

  var $PRODUCTNAME='';
  var $DESCRIPTION='';
  var $CATEGORY='';
  var $SUBCAT='';
  var $IMAGE='';




public function addproductm()
{
$this->load->database();
$data = array(
"p_name" => $this->PRODUCTNAME,
"p_des" => $this->DESCRIPTION,
"cat" => $this->CATEGORY,
"subcat" => $this->SUBCAT

);
$this->db->insert('product_multi', $data);
$lastid=$this->db->insert_id();
echo "$lastid";
return $lastid;

}


public function addimage()
{
$last=$this->addproductm();
$this->load->database();

$data = array(
"pid" =>$last,
"images" => $this->IMAGE
);
$this->db->insert('image', $data);
}


}

这是您所说的编辑后的代码...在单个函数中都插入 enter image description here

enter image description here

示例:

enter image description here

1 个答案:

答案 0 :(得分:1)

public function upload(){

//image upload  
     $config['upload_path'] = './images';
     $config['allowed_types'] = 'gif|jpg|png|jpeg';
     $this->load->library('upload',$config);
     $this->upload->initialize($config);

     $fileInfos = array();
     $errors = array();

     //uploading

     if (! empty($_FILES['images']['name'])) 
     {
        $photosCount = count($_FILES['images']['name']);
        for ($i = 0; $i < $photosCount; $i ++)
        {
           $_FILES['images']['name'] = $_FILES['images']['name'][$i];
           $_FILES['images']['type'] = $_FILES['images']['type'][$i];
           $_FILES['images']['tmp_name'] = $_FILES['images']['tmp_name'][$i];
           $_FILES['images']['error'] = $_FILES['images']['error'][$i];
           $_FILES['images']['size'] = $_FILES['images']['size'][$i];
           if ($this->upload->do_upload('images')) 
           {
                $data = $this->upload->data();
                array_push($fileInfos, $data['file_name']);
            }
            else{
                array_push($errors, array(
                    'error' => $this->upload->display_errors()
                ));
            }
        }

    }
        $product_array = array(
            "p_name" => $this->input->post("name"),
            "p_des" =>  $this->input->post("pdes"),
            "cat" => $this->input->post("category"),
            "subcat" => $this->input->post("subcat")
        );

       $this->ProductmModel->addproductm($product_array, $fileInfos);

     }

模型

     public function addproductm($product_array, $fileInfos){
        $this->load->database();

        $this->db->insert('product_multi', $product_array);
        $lastid = $this->db->insert_id();

        foreach($fileInfos as $file){
            $image_data = array(
                "pid" => $lastid,
                "images" => $file
            );
        $this->db->insert('image', $image_data);
        }

    }