TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Multi insert from form data (http://www.talkphp.com/general/5658-multi-insert-form-data.html)

maeltar 12-26-2010 01:49 PM

Multi insert from form data
 
Am a bit lost on how to get the data from a form and put it into a database (not update)

The mysql side easy enough..
Code:

insert into (col1, col2,col3) VALUES ($col1, $col2,$col3), ($col1, $col2,$col3). ($col1, $col2,$col3);
But it's getting the data from the form fields that am having problems getting my head round ..

So Multiple instances of col1, col2, col3 .. On Submit are they summitted as an array ?
e.g

Code:

<form id="form1" name="form1" method="post" action="<? $_SERVER['PHP_SELF']; ?>">
  <label>col1
  <input type="text" name="col1" id="col1" />
  </label>
  <label>col2
  <input type="text" name="col2" id="col2" />
  </label>
  <label>col3
  <input type="text" name="col3" id="col3" />
  </label>
  <p>
    <label>col1
    <input type="text" name="col4" id="col4" />
    </label>
    <label>col2
    <input type="text" name="col4" id="col5" />
    </label>
    <label>col3
    <input type="text" name="col4" id="col6" />
    </label>
</p>
  <p>
    <label>col1
    <input type="text" name="col5" id="col7" />
    </label>
    <label>col2
    <input type="text" name="col5" id="col8" />
    </label>
    <label>col3
    <input type="text" name="col5" id="col9" />
    </label>
</p>
  <p>
    <label>col1
    <input type="text" name="col6" id="col10" />
    </label>
    <label>col2
    <input type="text" name="col6" id="col11" />
    </label>
    <label>col3
    <input type="text" name="col6" id="col12" />
    </label>
</p>
  <p>
    <label>col1
    <input type="text" name="col7" id="col13" />
    </label>
    <label>col2
    <input type="text" name="col7" id="col14" />
    </label>
    <label>col3
    <input type="text" name="col7" id="col15" />
    </label>
</p>
  <p>
    <label>col1
    <input type="text" name="col8" id="col16" />
    </label>
    <label>col2
    <input type="text" name="col8" id="col17" />
    </label>
    <label>col3
    <input type="text" name="col8" id="col18" />
    </label>
</p>

 <p><input type="submit" name="Submit" id="Submit" value="Submit" /></p>

</form>


maeltar 12-26-2010 02:46 PM

Ok, so I now know how to get the form data into arrays...

Code:

<body>
<form id="form1" name="form1" method="post" action="<? $_SERVER['PHP_SELF']; ?>">
  <label>col1
  <input type="text" name="col1[]" id="col1" />
  </label>
  <label>col2
  <input type="text" name="col2[]" id="col2" />
  </label>
  <label>col3
  <input type="text" name="col3[]" id="col3" />
  </label>
  <p>
    <label>col1
    <input type="text" name="col1[]" id="col4" />
    </label>
    <label>col2
    <input type="text" name="col2[]" id="col5" />
    </label>
    <label>col3
    <input type="text" name="col3[]" id="col6" />
    </label>
</p>
  <p>
    <label>col1
    <input type="text" name="col1[]" id="col7" />
    </label>
    <label>col2
    <input type="text" name="col2[]" id="col8" />
    </label>
    <label>col3
    <input type="text" name="col3[]" id="col9" />
    </label>
</p>
  <p>
    <label>col1
    <input type="text" name="col1[]" id="col10" />
    </label>
    <label>col2
    <input type="text" name="col2[]" id="col11" />
    </label>
    <label>col3
    <input type="text" name="col3[]" id="col12" />
    </label>
</p>
  <p>
    <label>col1
    <input type="text" name="col1[]" id="col13" />
    </label>
    <label>col2
    <input type="text" name="col2[]" id="col14" />
    </label>
    <label>col3
    <input type="text" name="col3[]" id="col15" />
    </label>
</p>
  <p>
    <label>col1
    <input type="text" name="col1[]" id="col16" />
    </label>
    <label>col2
    <input type="text" name="col2[]" id="col17" />
    </label>
    <label>col3
    <input type="text" name="col3[]" id="col18" />
    </label>
</p>

 <p><input type="submit" name="Submit" id="Submit" value="Submit" /></p>

</form>

So I now have arrays col1[] col2[] col3[]

Hmm...

so have created a mutlidimentional array
Code:

$new_array = array(($_REQUEST['col1']),($_REQUEST['col2']),($_REQUEST['col3']));

maeltar 12-26-2010 02:53 PM

Now to show itterate though the array and show the data...

Code:

for ($i=0; $i < count($_REQUEST['col1']); $i++) {
    echo $_REQUEST['col1'][$i]." -- ";
    echo $_REQUEST['col2'][$i]." -- ";
    echo $_REQUEST['col3'][$i]."<br />";
}


maeltar 12-26-2010 03:14 PM

Time to build the sql ...
Code:

$sql = "INSERT INTO table(col1, col2, col3) VALUES ";

$columns = array(($_REQUEST['col1']),($_REQUEST['col2']),($_REQUEST['col3']));


for ($i=0; $i < count($_REQUEST['col1']); $i++) {
        $sql .= "(";
    $sql .= $_REQUEST['col1'][$i]. ",";
    $sql .= $_REQUEST['col2'][$i]. ",";
        $sql .= $_REQUEST['col3'][$i]. "),";
}

// Lets have a look at the final sql..
echo $sql . "<br />";

So, need to take the , off the end of the sql...

Code:

echo substr($sql, 0, -1);

And I think thats it about done..

Sorry for going on, but I thought I would ask the question, then figured out how to do it, I hope someone else gets some use out of this :-D


P.S. Not tried it yet so don't know if it works !


All times are GMT. The time now is 02:40 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0