0

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

1 Answer 1

1

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) {....
Sign up to request clarification or add additional context in comments.

7 Comments

What if I want to upload a file like this? <input type="file" name="file[]">
Thanks bro.. its working for me. But I can't insert the file names to my DB. Can you please help me. This is my complete code Code
can you help me to do this?
please check my link given already, I think this is the solution you are looking for
I check that link but I couldn't find solution for this issue. The only problem is I'm not getting the file name
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.