简体   繁体   中英

How To Insert Data Using Multiple Input With Same Name in codeigniter

I'm using JavaScript would produce the following entries

<input type="hidden" name="my_img[]" value="dad6e52b274690409835.jpg">
<input type="hidden" name="my_img[]" value="252529b6e21f872c7675.jpg">
<input type="hidden" name="my_img[]" value="3cce4128c366216fsfaf.jpg">
<input type="hidden" name="my_img[]" value="48697e8516caa3cc15d4.jpg">

But I've had trouble inserting them in the database

my controllers :

   $dd=$this->input->post('my_img');
   for($i=0;$i<count($dd);$i++)
   {
   $img_box[$i] = array ('img_url' => $dd[$i]);
   }
   $this->posts_model->add_new_ads_img($img_box);

my model :

public function add_new_ads_img($img_box)
{
    $q = $this->db->insert('advertise_gallery',$img_box);
    if ($q)
    {
        return TRUE;
    }
    else return FALSE;
}

And to eat error

Severity: Notice

Message: Undefined variable: img_box

Please guide me

The error could be this line in your controller:

$img_box[$i] = array ('img_url' => $dd[$i]);

Just before your for-loop add:

$img_box = [];

Based on what I think you are trying to do, you could try to give each input a different id. That way you could insert based on the id name.

for exampe:

    <input type="hidden" name="my_img[]" id="img1" value="dad6e52b274690409835.jpg">
<input type="hidden" name="my_img[]" id = "img2" value="252529b6e21f872c7675.jpg">
<input type="hidden" name="my_img[]" id = "img3" value="3cce4128c366216fsfaf.jpg">
<input type="hidden" name="my_img[]" id = "img4" value="48697e8516caa3cc15d4.jpg">

if you need to add them to an array, you can do so in the java script

var img=[$('#img1').val(),$('#img2').val(),$('#img3').val()]

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM