Help Please : ) text box loop problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gavin1512
Forum Newbie
Posts: 6
Joined: Mon Mar 10, 2008 1:46 pm

Help Please : ) text box loop problem

Post by gavin1512 »

Good Morning,

I am working on a basic internal order system with my SQL database backbone. The problem I am having is when writing from my array to the database. In the basket page it displays all items added to the basket array, and next to each item is a textbox to enter the quantity, when writing to the database it on takes the value in the last quantity box and inserts that value for all products.

Here is my code for writing to the database:

Code: Select all

 
<?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")."')";
    $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();
?>
 
 
And currently the text box is just declared as <input name="quantity" type="text">
I not sure if I need a foreach loop to insert the quantity or the box needs to be renamed to include the $key or if it can be related to the productnumber field
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Help Please : ) text box loop problem

Post by alex.barylski »

Not sure I follow 100% -- if you have multiple form fields all named quantity and you submit your products to database, the quantity value in the last FORM should be the one which all your products inherit, is this correct?
gavin1512
Forum Newbie
Posts: 6
Joined: Mon Mar 10, 2008 1:46 pm

Re: Help Please : ) text box loop problem

Post by gavin1512 »

sorry for being unclear

The basket displays all the products in the basket array, and for each product a quantity box is displayed. The user can then enter how many of each product they require next to each product, then will click the submit button to confirm the order, writing it to the database

The problem I am faced with is that the GET statement I am using is only getting the value from the final textbox and inserting that for every value, instead of getting values from all the textboxes

I did try to create a foreach loop to get all the individual values and insert them into the array but not sure if it was my coding or if this just was not feasible
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Help Please : ) text box loop problem

Post by alex.barylski »

The problem I am faced with is that the GET statement I am using is only getting the value from the final textbox and inserting that for every value, instead of getting values from all the textboxes
Thats what I thought.

You need to make quantity an array.

Code: Select all

<input type="text" name="quantity[]" value="" />
Not sure if that will work -- you may need to:

Code: Select all

<input type="text" name="quantity_x" value="" />
As you iterate your array, you need to index each elements details for each record -- make sense?
gavin1512
Forum Newbie
Posts: 6
Joined: Mon Mar 10, 2008 1:46 pm

Re: Help Please : ) text box loop problem

Post by gavin1512 »

Thank you for your assistance

Sorry I am a fairly basic using doing this as part of a university project

I have tried both methods but entirely sure on how index each, is it possible to relate it back to the primary key? or is it by its position in the array
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Help Please : ) text box loop problem

Post by alex.barylski »

What does your FORM look like?

Remember to wrap it in HTML tags. ;)
gavin1512
Forum Newbie
Posts: 6
Joined: Mon Mar 10, 2008 1:46 pm

Re: Help Please : ) text box loop problem

Post by gavin1512 »

This is my basket form minus the SQL eetc. at the top, is this ok?

Thanks for your assistance your a life saver!

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<!-- TemplateBeginEditable name="doctitle" -->
<title>Untitled Document</title>
<!-- TemplateEndEditable -->
<!-- TemplateBeginEditable name="head" --><!-- TemplateEndEditable -->
<style type="text/css">
<!--
#Layer1 {
    position:absolute;
    width:988px;
    height:680px;
    z-index:1;
    left: 215px;
    top: 104px;
}
#Layer2 {
    position:absolute;
    width:200px;
    height:435px;
    z-index:2;
    left: 15px;
    top: 108px;
}
#Layer3 {
    position:absolute;
    width:200px;
    height:51px;
    z-index:3;
    left: 6px;
    top: -86px;
}
#Layer4 {
    position:absolute;
    width:267px;
    height:39px;
    z-index:3;
    left: 713px;
    top: -73px;
}
.style1 {font-family: Arial, Helvetica, sans-serif}
.style2 {font-size: small}
-->
</style>
</head>
 
<body>
<p class="style1 style1"><img src="back.jpg" width="1199" height="652" align="top" /></p>
<?php
        
        switch ($_SESSION['access']) 
    {
            case 'User':
            include('user side.php');
            break;
  
            case 'Manager':
            include('manager side.php');
            break;
  
            case 'Fulfilment':
            include('fulfil side.php');
            break;
 
            case 'Admin':
            include('admin side.php');
            break;
    }
?>
<div class="style1 style1" id="Layer1">
<form action="confirmorder.php" method="get">
  <table width="820" border="0">
    <tr>
      <td width="157">&nbsp;</td>
      <td width="184">&nbsp;</td>
      <td width="181">&nbsp;</td>
      <td width="187">&nbsp;</td>
      <td width="89">&nbsp;</td>
    </tr>
    <tr>
      <td><div id="Layer3">
        <table width="200" border="0">
          <tr>
            <td height="38"><strong>Basket</strong></td>
          </tr>
        </table>
      </div></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td><strong>Product Number </strong></td>
      <td><strong>Type</strong></td>
      <td><strong>Description</strong></td>
      <td><strong>Price</strong></td>
      <td><strong>Quantity</strong></td>
      <td><strong>Remove</strong></td>
 
    </tr>
    <tr>
      <?php
      if (isset($_SESSION['order'])){ ?>
    <?php
        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>&pound;<?php echo $_SESSION['order'][$key]['price']?></td>
      <td width="198"><input name="quantity" type="text"></td>
      <td width="9"><div align="center"><a href="remove3.php?productnumber=<?php echo $product['productnumber']?>" class="style2"> Remove </a></div></td>
    </tr>
      <?php } ?>
  </table>
        <?php }
    else { ?>
        No products chosen yet
        <?php } 
    ?></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
 
  </table>
  <table width="819" height="25" border="0">
  
    <tr>
      <td width="51">&nbsp;</td>
      <td width="51">&nbsp;</td>
      <td width="377"><div align="right">
        <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" width="101" height="20">
          <param name="movie" value="text29.swf" />
          <param name="quality" value="high" />
          
          <embed src="text29.swf" quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="101" height="20"></embed>
        </object>
      </div></td>
      <td width="231"><div align="right">
        <?php
    echo("Date: ".date("d M Y") . "<br />");
?>
      </div>
        <div align="right"></div></td>
      <td width="87"><div align="right">
        <input name="confirm" type="submit" value="Save" />
      </div></td>
      
    </tr>
  </table>
  </form>
  <p>&nbsp;</p>
  
  
</div>
<p class="style1 style1">&nbsp;</p>
</body>
</html>
Post Reply