Jump to content

Reading from textbox loop


gavin1512

Recommended Posts

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>

Link to comment
Share on other sites

<?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']."')";
}

?>

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.