Jump to content

array from form


bradynorman

Recommended Posts

I have created a form with a max of 10 rows, some rows may not be used/filled in

 

I need to insert the populated rows into a mysql db, but i'm strugling to get my head aroung creating an array with which to do this.

 

Am i trying to create the impossible? any hints or tips will be greatly appreciated.

thanks

Mike

Link to comment
https://forums.phpfreaks.com/topic/117971-array-from-form/
Share on other sites

you can make arrays in $_POST by setting up form inputs to be

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

and then in php you can loop it using a foreach

<?php
foreach($_POST['rows'] as $key=> $value){
if(!empty($value)){
#insert
}
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/117971-array-from-form/#findComment-606904
Share on other sites

Please post some code...

If I'm understanding you then you have a form with 10 elements, each of which needs to be inserted into a database.

If I am correct then

<?php
$line = mysql_connect() or die(mysql_error());
mysql_select_db('dbname') or die(mysql_error());
$sql = "INSERT INTO tableName VALUES ('', '" . mysql_escape_string($_POST['varName']) . '", '" . mysql_escape_string($_POST['varName2']) . "')";
mysql_query($sql, $link) or die(mysql_error());
?>

Link to comment
https://forums.phpfreaks.com/topic/117971-array-from-form/#findComment-606906
Share on other sites

Thank you all for your replies to date, i'll post some more info that may help.

 

This is a snippet of the code, in total there are ten rows each containing 6 elements - ponumber, line, pn, qty, date_ordered and status - The db table has an additional field 'id' - auto increment etc.

 

<input name="po[]" type="hidden"  value="<? echo $ponumber ;?>" readonly="<? echo $ponumber ;?>" /><? echo $ponumber ;?>/

<input name="line[]" type="text"  value="01" size="2" readonly="<? echo $ponumber ;?>" /></td>

<td><input name="pn[]" type="text" size="25" /></td>

<td><input name="qty[]" type="text"  size="3" />

<input name="date_ordered[]" type="hidden" id="date_ordered" value="<? print date("Y-m-j"); ?>" />

<input name="status[]" type="hidden"  value="pending" /></td>

 

<input name="po[]" type="hidden" value="<? echo $ponumber ;?>" readonly="<? echo $ponumber ;?>" /><? echo $ponumber ;?>/

<input name="line[]" type="text" value="02" size="2"readonly="<? echo $ponumber ;?>" /></td>

<td><input name="pn[]" type="text"  size="25" /></td>

<td><input name="qty[]" type="text"  size="3" />

<input name="dare_ordered[]" type="hidden"  value="<? print date("Y-m-j"); ?>" />

<input name="status[]" type="hidden" id="status" value="pending" /></td>

 

 

How do i cycle through each line to INSERT them into the db.

 

cheers

Mike

Link to comment
https://forums.phpfreaks.com/topic/117971-array-from-form/#findComment-606927
Share on other sites

put them in a 3d array would be better

<input name="inputs[0]['po']" type="hidden"  value="<? echo $ponumber ;?>" readonly="<? echo $ponumber ;?>" /><? echo $ponumber ;?>/
<input name="inputs[0]['line']" type="text"  value="01" size="2" readonly="<? echo $ponumber ;?>" /></td>
<td><input name="inputs[0]['pn']" type="text" size="25" /></td>
<td><input name="inputs[0][qty']" type="text"  size="3" />
<input name="inputs[0]['date_ordered']" type="hidden" id="date_ordered" value="<? print date("Y-m-j"); ?>" />
<input name="inputs[0]['status']" type="hidden"  value="pending" /></td>

then to work on it

 

<?php
foreach($_POST['inputs'] as $key=>$value){
print_r($value);
}
?>

to get an idea

Link to comment
https://forums.phpfreaks.com/topic/117971-array-from-form/#findComment-606935
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.