ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

How can insert values array as batch in database mysql?

October 17, 2011 4:28am

Subscribe [2]
  • #1 / Oct 17, 2011 4:28am

    SaSa

    118 posts

    For example i have following values and want insert batch they in database, How is it?(can done it with foreach)

    My values:

    <input name="u_id[]" value="76">
    
    <input name="un[0][0]" 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[1][2]" value="3">
    <input type="text" name="ue[]" value="33">
    <input type="text" name="up[]" value="333">

    insert batch in database as:

    http://i.imgur.com/H6EDa.png

  • #2 / Oct 17, 2011 11:11am

    Alexandru

    22 posts

    where is your problem?

  • #3 / Oct 17, 2011 11:27am

    SaSa

    118 posts

    I want know, how should insert batch above values in database table like this: http://i.imgur.com/H6EDa.png

    my php code is this but not work it:

    $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);

    How should change html code(input:name)(can not change input value) and what use in php code? What do i do?

  • #4 / Oct 17, 2011 12:02pm

    Alexandru

    22 posts

    You try to insert an array into the database here :

    [un] => Array ( [0] => 1 )

    I am sure there is a better way to handle what you try.

  • #5 / Oct 17, 2011 12:10pm

    Alexandru

    22 posts

    try this html code

    <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"> 
    
    <h2>2</h2>
    <p><input name="u_id[]" value="77">   </p>
    
    <p><input name="un[]" value="2"> <br />
    <input type="text" name="ue[]" value="22">  <br />
    <input type="text" name="up[]" value="222"> </p>
    
    <h2>3</h2>
    <p><input name="u_id[]" value="77">   <br />
    <input name="un[]" value="2"> <br />
    <input type="text" name="ue[]" value="22">  <br />
    <input type="text" name="up[]" value="222"> <br />
    <input type="submit" value="Send it">

    Then you’ll get a nice post request

    Array ( [u_id] => Array ( [0] => 76 [1] => 77 [2] => 77 ) [un] => Array ( [0] => 1 [1] => 2 [2] => 2 ) [ue] => Array ( [0] => 11 [1] => 22 [2] => 22 ) [up] => Array ( [0] => 111 [1] => 222 [2] => 222 ) )

    Now you can use these values to do your db queries ,
    rebuild the array for the batch using a for loop .

    The validation will be tricky.

  • #6 / Oct 17, 2011 12:23pm

    SaSa

    118 posts

    How is php it, my php not work!?

  • #7 / Oct 17, 2011 12:30pm

    Alexandru

    22 posts

    the php I did is

    print_r($this->input->post());

    this code shows you all post data you have .. so now it has a correct order (with the changed names in html)

    push the $_POST[] variables through a small for-loop and build an array that has the correct db syntax.


    if you need help, write here.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases