SocomNegotiator Posted July 21, 2008 Share Posted July 21, 2008 Ok here is my while statement that each item from the DB displays in a form: <?php while($row = $db->fetch_array()) { ?> <form id='reorder_form' action='?page=reorder' method='post'><input type='hidden' name='order_number' value='<?=$row['order_number']?>'/> <li> <li>Item Name: <?=$row['name']?></li><br /> <ul style="width: 200px; height: 125px;"> <li>Quantity: <input type='text' name='amount' size='5' value='<?=$row['amount']?>' /><br /></li> </ul> </li> <?php } ?> </ul> <br style="clear: left" /> </div> <center><input type='submit' name='submit' value='Re-Submit Order'/></center> </form> </div> Now you notice that the Quantity: is the only thing the user can change. Now how would I go about getting the quantity of the results from the while statement if there is more than one result and the form is submitted? For instance, I can do this on submit $amount = $_POST['amount'], but the post back for amount will be the first items quantity only. So how would I go about getting the second, third, fourth, etc quantities if there were more than one result that came back from the while loop? I know I can change: this: <li>Quantity: <input type='text' name='amount' size='5' value='<?=$row['amount']?>' /><br /></li> to this: <li>Quantity: <input type='text' name='amount<?=$row['id']?>' size='5' value='<?=$row['amount']?>' /><br /></li> But if there were two items that were being $_POST back how would I get the result so that I can insert them into the DB with the correct quantity and item. Here is my post-submit code: <?php //BEGIN: Form Processing: if(isset($_POST['submit'])) { $order_number = $_POST['order_number']; //This $_POST will only get one items amount how can I get both if there were two. And then how to insert it into the DB with the correct item. $amount = $_POST['amount']; //This just gets the last order to be submitted id...and then adds one to it for the new orders order_number. $db->query("SELECT * FROM `order` ORDER BY order_number DESC LIMIT 1") or die(mysql_error()); $number = $db->fetch_array(); $num = 1; if($num = $number['order_number']) $num = $number['order_number'] + 1; elseif($number['order_number'] == "") $num = 1; else $num = $num; //This is where I select based on order_number and I insert into the order table. But how can I get the quantity if there were two or more items that were posted back..? $res = mysql_query("SELECT * FROM `order` WHERE order_number = '$order_number'") or die(mysql_error()); while($r = mysql_fetch_array($res)){ $inserted=$db->query("INSERT INTO `order` (`order_number`,`pre_order_id` , `item_id` , `amount` , `user_id` ) VALUES (".$num.",".$r['pre_order_id'].", ".$r['item_id'].", ".$amount.", ".$r['user_id'].")") or die (mysql_error()); // track the id's of the new inserts $insert_ids[] = mysql_insert_id(); } if($error){ echo $error; // remove any rows that HAVE been inserted from the pre_order table $ids = implode("', '", $insert_ids); if($inserted){ echo "Your order has been re-submitted."; } else { echo "Could not re-submit your order Error: ".mysql_error(); } } else { if($inserted){ echo "Your order has been re-submitted."; } else { echo "Could not re-submit your order Error: ".mysql_error(); } } } Link to comment https://forums.phpfreaks.com/topic/115879-solved-phpmysql-form-submit/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.