4

I want insert following data by insert_batch as in following example in database table (mysql):

HTML:

<input name="u_id[0][0]" value="76">

<input name="un[0][0]" value="1">
<input type="text" name="ue[0][0]" value="11">
<input type="text" name="up[0][0]" value="111">



<input name="u_id[1][0]" value="77">

<input name="un[1][1]" value="2">
<input type="text" name="ue[1][1]" value="22">
<input type="text" name="up[1][1]" value="222">

<input name="un[1][2]" value="3">
<input type="text" name="ue[1][2]" value="33">
<input type="text" name="up[1][2]" value="333">

PHP:

$u_id       = $this->input->post('u_id');
$un       = $this->input->post('un');
$up       = $this->input->post('up');
$ue       = $this->input->post('ue');

$data = array();
foreach ($un as $idx => $name) {
    $data[] = array(
        'u_id' => $u_id[$idx],                  
        'un' => $un[$idx],
        'up' => $up[$idx],
        'ue' => $ue[$idx],
    );
};
$this -> db -> insert_batch('units', $data);

I want insert they as this:

enter image description here

How should change php code and html code? what do i do?

4
  • 1
    You are using a framework and we don't know which one, at least give us the framework name cause i don't know where that "insert_batch" comes from, it is not standard PHP Commented Oct 17, 2011 at 0:06
  • Exact duplicate of How can insert values array as batch in database mysql? Commented Oct 17, 2011 at 8:19
  • @hakre - It not better that help me instead of chase me!!!! Commented Oct 17, 2011 at 15:34
  • @Mathieu Dumoulin - how is without framework? Commented Oct 17, 2011 at 15:35

2 Answers 2

1

I am assuming you are using CodeIgniter and that the name of the database table that you want to insert to is called 'units' and that its 'id' column is autoincrement.

I am basing off my solution from CodeIgniter User Guide Version 2.0.3 using a call to a helper ($this->db->insert_string()) of the Database class.

foreach ($data as $row)
{
    $error_code = $this->db->insert_string('units', $row); 
}

Refer to http://codeigniter.com/user_guide/database/helpers.html

The insert_string function that the Database class provides appears to take an associative array, build an INSERT statement from the elements inside, execute it and then return a numerical error code.

Sign up to request clarification or add additional context in comments.

Comments

0

LOL, it's not pretty, but it might work sometimes:

<input name="u_id[]" value="76">

<input name="un[]" value="1">
<input type="text" name="ue[]" value="11">
<input type="text" name="up[]" value="111">



<input name="u_id[]" value="77">

<input name="un[]" value="2">
<input type="text" name="ue[]" value="22">
<input type="text" name="up[]" value="222">

<input name="un[]" value="3">
<input type="text" name="ue[]" value="33">
<input type="text" name="up[]" value="333">

$u_id=$this->input->post('u_id');
$un=$this->input->post('un');
$up=$this->input->post('up');
$ue=$this->input->post('ue');
for($i=0;$i<count($u_id);$i++){
    for($ii=0;$ii<count($un[$i]);$ii++){
        (count($un[$i])>1)?$unn=$un[$i][$ii+1]:$unn=$un[$i][$ii];
        (count($ue[$i])>1)?$uen=$ue[$i][$ii+1]:$uen=$ue[$i][$ii];
        (count($up[$i])>1)?$upn=$up[$i][$ii+1]:$upn=$up[$i][$ii];
        $this->db->insert('units', array(//use db insert here
            'u_id'=>$u_id[$i][0],
            'un'=>$unn,
            'ue'=>$uen,
            'up'=>$upn,
        ));
    }
}

I'd go so far as to suggest you not use it. But perhaps it might inspire someone to offer a better solution.

Cheers.

2 Comments

I don't understand your question.
Your php code not work true. i want insert above values like this image in database table: i.sstatic.net/AjwXP.png . Where you do not understand from my question?

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.