gavin1512 Posted March 2, 2008 Share Posted March 2, 2008 Good Afternoon people, I am currently working on a basic internal ordering system, the products are read from the database then if the user wants the product it is added to an array. When they go to the check out they have the option of adding a quantity, the problem I am having is that when I confirm the order and write it to the orders table it will only read the first value and repeat that. I have tried various loops to add the quantity to the array of straight to the orders database. This in my current confirmorder.php script: <?php session_start(); require "connect.php"; $quantity = $_GET['quantity']; $username = $_SESSION['username']; $order = $username.date("d/m/y : H:i:s", time()); $query = "insert into orders values ('".$order."','".$username."','pending approval','".date("d-M-Y")."',1000)"; $result = mysql_query($query, $connection) or die (mysql_error()); $orderid = mysql_insert_id($connection); foreach($_SESSION['order'] as $key => $product) { $query = "insert into productorder values ('".$order."','".$_SESSION['order'][$key]['productnumber']."','".$quantity."', 'pending approval','".$_SESSION['order'][$key]['price']."')"; $result = mysql_query($query, $connection) or die ("Unable to perfom query<br>$query"); } unset($_SESSION['order']); $message3 = "Your Order has been sent to your manager for approval"; header("Location: productsearch.php?var=productnumber&message3=$message3"); exit(); ?> I am guessin the text box name with have to be altered to reference the key, at the moment it is just <td width="198"><input name="quantity" type="text"></td> Quote Link to comment https://forums.phpfreaks.com/topic/94003-reading-from-textbox-loop/ Share on other sites More sharing options...
ignace Posted March 2, 2008 Share Posted March 2, 2008 <?php foreach($_SESSION['order'] as $key => $product) { $query = "insert into productorder values ('".$order."','".$_SESSION['order'][$key]['productnumber']."','".$quantity."', 'pending approval','".$_SESSION['order'][$key]['price']."')"; ?> should be <?php for ($i = 0; $i < count($_SESSION['order']); $i++) { $query = "insert into productorder values ('".$order."','".$_SESSION['order'][$i]['productnumber']."','".$quantity."', 'pending approval','".$_SESSION['order'][$i]['price']."')"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/94003-reading-from-textbox-loop/#findComment-481631 Share on other sites More sharing options...
gavin1512 Posted March 2, 2008 Author Share Posted March 2, 2008 No sorry i do not think I have made myself clear with my problem, i do not wish to increment the value by one when repeated add, there is a text box where the quantity is manually written next to each product. If i were to enter 20 10 and 15 against 3 products it will just write 20 3 times a there is no for each loop associated with the productnumber [key] Quote Link to comment https://forums.phpfreaks.com/topic/94003-reading-from-textbox-loop/#findComment-481639 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.