Shihas
Shihas

Reputation: 814

Codeigniter insert_batch issue

Im trying to insert multiple value to database using insert_batch. But its showing me error.

Controller:

    public function add()
    {
        $data['title'] = $this->input->post('title');
        $data['file'] = $this->input->post('file');
        $this->user->in();
    }  

Model:

 public function in()
 {
        $data['title'] = $this->input->post('title');
        $data['file'] = $this->input->post('file');

        foreach ($title as $key => $n) {

                        $insert = array(
                                'title' => $n,
                                'file' => $file[$key]
                                );
                        // echo $n."->".$file[$key].'<br>' ;
            }
            $this->db->insert_batch('title',$insert);  //line 42 in user.php (error)
            // return $query;
 }  

view:

<?php echo form_open('location/add'); ?>
<div>
  <input type="text" name="title[]"><br>
  <input type="text" name="file[]">
</div>
<br><br>
<div>
  <input type="text" name="title[]"><br>
  <input type="text" name="file[]">
</div>   <br><br>   
<div>
  <input type="text" name="title[]"><br>
  <input type="text" name="file[]">
</div>
<input type="submit" name="" value="enter">
<?php echo form_close(); ?>  

But when I run this it's showing me error like this:

A Database Error Occurred

You must use the "set" method to update an entry.

Filename: models/User.php

Line Number: 47

Upvotes: 0

Views: 903

Answers (1)

Rejoanul Alam
Rejoanul Alam

Reputation: 5398

Instead of this

$insert = array(
   'title' => $n,
   'file' => $file[$key]
);

It should be like following

 $insert[] = array(
     'title' => $n,
     'file' => $data['file'][$key]
 );

and instead of this

 foreach ($title as $key => $n) {...

Use

 foreach ($data['title'] as $key => $n) {....

Upvotes: 1

Related Questions