Hangston Posted May 15, 2010 Share Posted May 15, 2010 I use the following code to pull in data and create a form. This creates up to 40 lines for a user to fill out. Each line consists of the same information: Description, Amount, and Frequency. The remainder of the information needed is generated by the database. (See hidden fields) <table> <?php $rows = 0; $i = 0; do { ?> <?php $optid = $row['option_id'];?> <tr> <td><?php echo htmlentities($row['option'])?> </td> <td><input name="description[]" type="text" size="40" maxlength="120"/> </td> <td><input name="option_id[]" type='hidden' value="<?php echo $optid; ?>" />$<input name="amount[]" type="text" size="10" maxlength="7"/> </td> <td><select name="assisted_frequency[]"> <option value="Monthly">Monthly</option> <option value="Weekly">Weekly</option> <option value="Daily">Daily</option> <option value="Hourly">Hourly</option> <option value="One-Time">One-Time</option></select> </td> </tr> <?php $rows ++; $i ++; ?> <?php } while ($row_options = mysql_fetch_assoc($options));?> <?php $assistcount = $row -1; ?> </table> <center><input type="hidden" name="MM_insert" value="form1" /><input type="submit" value="Insert record" /></center> </form> I'm having troubles retrieving the information that the user inputs into this loop/database generated form. My intent is to loop through each row after the user has input their information, then upload the mix of my database information and the user's information into another database. For example, the user would see, albeit prettier: form1 Option 1: description [input box] amount [input box] frequency [option box] Option 2: description [input box] amount [input box] frequency [option box] Option 3: description [input box] amount [input box] frequency [option box] Option 4: description [input box] amount [input box] frequency [option box] submit Upon submitting the form above, I'm using a query similar to the following to input the data into the database: <?php // on submit for ($i = 0; $i < $assistcount; $i++) { $insertSQL2 = sprintf("INSERT INTO table (id, option_id, amount, description, frequency) VALUES (%s, %s, %s, %s, %s)", GetSQLValueString($id, "int"), GetSQLValueString($_POST['option_id[$i]'], "int"), GetSQLValueString($_POST['amount[$i]'], "int"), GetSQLValueString($_POST['description[$i]'], "text"), GetSQLValueString($_POST['frequency[$i]'], "text")); mysql_select_db($database_dbname, $database); $Result2 = mysql_query($insertSQL2, $database) or die(mysql_error()); ?> I am having problems posting each row (row by row) into the database. I either get just the last row of data, or no data at all. I also need to ensure that the $optid assigned to each row stays with that row, as it is an id used to identify the option in the database. Are you able to see where I have gone wrong? What is the best way to retrieve each row of the user's inputs, then upload this data (row by row) into the database? Thank you! Hangston Link to comment https://forums.phpfreaks.com/topic/201877-loop-through-form-and-insert-row-by-row-to-mysql/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.