0

I have got a form with some inputs like:

<form action="http://site.com" method="post" accept-charset="utf-8">
<input type="text" name="image_Title" value="Image" />
<input type="text" name="image_Basename" value="0000001" />
<input type="text" name="image_basename" value="0000002" />
<input type="text" name="image_basename" value="0000003" />
</form>

How can I insert each image_Id with the same image_Title to one row using Codeigniter?! I used foreach, but I couldn't make it work?!

for example:

table-row-name:image_Title              table-row-name:image_Basename
       'Image'                                '00000001'
       'Image'                                '00000002'
       'Image'                                '00000003'
       'Image2'                               '00000258'
       'Image2'                               '00000102'

How can I do that ?!

This is my php code ( of cource in Codeigniter):

foreach($_POST['image_Basename'] as $image_Basename) 
{
    $image_Id = $this->gallery_model->add(array('image_Title' => $_POST['image_Title'], 'image_Basename' => $image_Basename)); 
}     

My Model:

function add($options = array())
{

    $options = array(

    'image_Title' => $_Post['image_Title'],

    'image_Basename' => $image_Basename

    );

    $this->db->insert('mg_gallery', $options);

    return $this->db->insert_id();
}
2
  • 2
    How are you inserting? What programming language are you using? Commented Aug 20, 2013 at 7:07
  • I'm using PHP. I will test your solution and will inform you :) Commented Aug 20, 2013 at 8:14

1 Answer 1

1

If you are using PHP, add [] to the input names, and you'll receive an array of values.

In the HTML

<input type="text" name="image_Id[]" value="0000003" />

With MySQLi (no error checking, no connecting):

foreach ($_POST['image_Id'] as $id) {
    $stmt = $mysqli->prepare("INSERT INTO table (id) VALUES (?)");
    $stmt->bin_param("s", $id);
    $stmt->execute();
}
Sign up to request clarification or add additional context in comments.

4 Comments

then, how can I insert them?!
Show some code of what you have tried. Basically, you loop over the array you receive from $_POST and insert the values.
how should I insert name="name[]" in to the database in general?!
@Afshin I updated my answer. I don't know CodeIgniter, so I cannot help you with that. This is how it works in plain PHP.

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.