bradynorman Posted August 3, 2008 Share Posted August 3, 2008 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 More sharing options...
cooldude832 Posted August 3, 2008 Share Posted August 3, 2008 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 More sharing options...
unkwntech Posted August 3, 2008 Share Posted August 3, 2008 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 More sharing options...
unkwntech Posted August 3, 2008 Share Posted August 3, 2008 <input type="text" name="rows[]" value="" /> <input type="text" name="rows[]" value="" /> <input type="text" name="rows[]" value="" /> Typically it would be better to name each field something meaningful. Link to comment https://forums.phpfreaks.com/topic/117971-array-from-form/#findComment-606908 Share on other sites More sharing options...
cooldude832 Posted August 3, 2008 Share Posted August 3, 2008 what do you mean naming it meaningful? The name I gave "rows" is just because they didn't say what it is more than "10 rows" The keys are not required Link to comment https://forums.phpfreaks.com/topic/117971-array-from-form/#findComment-606913 Share on other sites More sharing options...
bradynorman Posted August 3, 2008 Author Share Posted August 3, 2008 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 More sharing options...
cooldude832 Posted August 3, 2008 Share Posted August 3, 2008 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 More sharing options...
bradynorman Posted August 3, 2008 Author Share Posted August 3, 2008 Thank you cooldude832 , that helps a lot. Can i see you cms please. cheers Mike Link to comment https://forums.phpfreaks.com/topic/117971-array-from-form/#findComment-606949 Share on other sites More sharing options...
cooldude832 Posted August 3, 2008 Share Posted August 3, 2008 I'm workin on rerolling it out in the next few weeks after alpha testing so I will update my link when its there Link to comment https://forums.phpfreaks.com/topic/117971-array-from-form/#findComment-606952 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.