title: While inserting multiple images to database more than one row are added in main table which is connected to images table
body: <p>I'm trying to upload multiple image in codeigniter. For a particular product I want to add the uploaded images to another table calles"image".... while using "lastid" so that I get pid(product id from table product_multi) in images multiples rows are inserted in the product_multi table... the number rows is according to the number of images I upload. <br>Someone help me to fix this.. If I'm not using lastid variable passing it works perfectly/... but I want pid in images tables :(</p>
  
  <p>This is how im getting datable<a href="https://i.stack.imgur.com/dcJe3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dcJe3.png" alt="This is how getting"></a></p>
  
  <p>This is my model</p>
  
  <pre><code>&lt;?php
  
  
  class ProductmModel extends CI_Model
  {
  
    var $PRODUCTNAME='';
    var $DESCRIPTION='';
    var $CATEGORY='';
    var $SUBCAT='';
    var $IMAGE='';
  
  
  
  
  public function addproductm()
  {
  $this-&gt;load-&gt;database();
  $data = array(
  "p_name" =&gt; $this-&gt;PRODUCTNAME,
  "p_des" =&gt; $this-&gt;DESCRIPTION,
  "cat" =&gt; $this-&gt;CATEGORY,
  "subcat" =&gt; $this-&gt;SUBCAT
  
  );
  $this-&gt;db-&gt;insert('product_multi', $data);
  $lastid=$this-&gt;db-&gt;insert_id();
  echo "$lastid";
  return $lastid;
  
  }
  
  
  public function addimage()
  {
  $last=$this-&gt;addproductm();
  $this-&gt;load-&gt;database();
  
  $data = array(
  "pid" =&gt;$last,
  "images" =&gt; $this-&gt;IMAGE
  );
  $this-&gt;db-&gt;insert('image', $data);
  }
  
  
  }
  </code></pre>
  
  <p>controller code::</p>
  
  <pre><code>public function upload()
     {
  
  
  
  //image upload  
       $config['upload_path'] = './images';
       $config['allowed_types'] = 'gif|jpg|png|jpeg';
       $this-&gt;load-&gt;library('upload',$config);
       $this-&gt;upload-&gt;initialize($config);
  
       $fileInfos = array();
       $errors = array();
  
       //uploading
  
       if (! empty($_FILES['images']['name'])) 
       {
          $photosCount = count($_FILES['images']['name']);
          for ($i = 0; $i &lt; $photosCount; $i ++)
           {
             $_FILES['image']['name'] = $_FILES['images']['name'][$i];
             $_FILES['image']['type'] = $_FILES['images']['type'][$i];
             $_FILES['image']['tmp_name'] = $_FILES['images']['tmp_name'][$i];
             $_FILES['image']['error'] = $_FILES['images']['error'][$i];
             $_FILES['image']['size'] = $_FILES['images']['size'][$i];
             if ($this-&gt;upload-&gt;do_upload('image')) 
             {
                      array_push($fileInfos, array(
                          'fileInfo' =&gt; $this-&gt;upload-&gt;data()
  
                      ));
                    $filename =$_FILES['image']['name'] ;
                      //$filename =$_FILES["name"].$_FILES["type"];
  
                        //   $data['fileInfos'] = $fileInfo;
                    $this-&gt;load-&gt;model("ProductmModel");
                          $this-&gt;ProductmModel-&gt;IMAGE = $filename;
  
  
              }
              else 
                  {
                      array_push($errors, array(
                          'error' =&gt; $this-&gt;upload-&gt;display_errors()
                      ));
                  }
  
  
             }
  
  
           }
  
  
        $pname = $this-&gt;input-&gt;post("name");
        $pdes = $this-&gt;input-&gt;post("pdes");
        $caty = $this-&gt;input-&gt;post("category");
        $subcat = $this-&gt;input-&gt;post("subcat");
  
          $this-&gt;ProductmModel-&gt;PRODUCTNAME = $pname;
          $this-&gt;ProductmModel-&gt;DESCRIPTION = $pdes;
          $this-&gt;ProductmModel-&gt;CATEGORY = $caty;
          $this-&gt;ProductmModel-&gt;SUBCAT = $subcat;
         $this-&gt;ProductmModel-&gt;addproductm();
  
  
       }
  </code></pre>
  
  <p>This is the code after editing as u said...making both insertion in single function
  <a href="https://i.stack.imgur.com/zybSV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zybSV.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/nfYT6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nfYT6.png" alt="enter image description here"></a></p>
  
  <p>exampleee:</p>
  
  <p><a href="https://i.stack.imgur.com/nODo2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nODo2.png" alt="enter image description here"></a></p>