Jump to content

Recommended Posts

Good Afternoon,

 

I am having a slight issue in writing mulitple text box entries to the database, basically I have a shopping basket and next to each item is a textbox with the name quantity, the items are displayed using a loop to display entries from an array:

 

foreach($_SESSION['order'] as $key => $product){ ?>

<tr>

      <td><?php echo $_SESSION['order'][$key]['productnumber']?></td>

  <td><?php echo $_SESSION['order'][$key]['type']?></td>

      <td><?php echo $_SESSION['order'][$key]['description']?></td>  

      <td>£<?php echo $_SESSION['order'][$key]['price']?></td>

      <td width="198"><input name="quantity" type="text"></td>

 

the problem I am having is that it will only take the first entry for quantity and put that against each item and ignore the other text boxes. I am just using a simple GET statement, does the GET statement need to be put into a loop?

Link to comment
https://forums.phpfreaks.com/topic/88054-multiple-text-box-get-statement-problem/
Share on other sites

Well, the name of the field needs to be quantity[id] here, for example:

<?php
foreach ($_SESSION['order'] as $key=>$product)
{
echo '<tr>
    <td>' . $product['productnumber'] . '</td>
    <td>' . $product['type'] . '</td>
    <td>' . $product['description'] . '</td>
    <td>£' . $product['price'] .'</td>
    <td width="198"><input name="quantity[' . $key . ']" type="text" /></td>
</tr>';
}
?>

Then your PHP script to access the fields would look like this:

<?php
foreach ($_GET['quantity'] as $key=>$quantity)
{
   // $key now relates to the key of the item in that session
   // i.e. if $key is 4, then you are editing the quantity for $_SESSION['order'][4]
}
?>

Thank you for your assistance, I have updated my form fine just having a slight problem on the script I have the foreach loop of:

{

$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");

}

 

 

would I need to put the foreach ($_GET['quantity'] as $key=>$quantity) loop before hand and update the ".$quantity." to $_GET['quantity'][$key]?

  • 1 month later...
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.