Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have a little store.
I want in the database to store the available amount of a product, and list it in a <select> statement, when the user choose from the select list , the number is stored in the database so that I can retrieve it later.

I want this process to repeat for all the products I have. 

I started the code , but I don't know how to store the selection that the user chooses.

<?php
mysql_connect("localhost");
mysql_select_db("db");
$sql = "SELECT * FROM store";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs)) 
{
   $p = $row[p];
   $available = $row[av];
   $price = $row[price];

   echo "<tr>

      <td>$p</td>
      <td>
         <select name=".$choice.">";
            for($i=0; $i <= $available; $i++)
            {
               echo "<option value=".$i." name=".$i.">$i</option>";
            } 
         echo "</select>
      <td>$price</td>
 
   </tr>";
}
mysql_close();
?> 


After that, when I click submit, I want a page that prints out every product and the amount that the user choose + the price and the total price

I tried to use the GET, POST but i didn't figure out where exactly should i put it, here or in the second file.

I hope you can help me.

Posted
Updated 21-Nov-09 3:02am
v3

I gave you some links, in your other question, which you should really take a look at.

As this question is more specific: below is an example form, simplified for what you have currently (as it is 'just' an example), which you should be able to figure out what to do, as everything you need will be in it (apart from the database side).

shop.php

PHP
<!-- Notice that the method used is "post", this hides the data submited in the background. -->
<!-- "get" on the otherhand, shows the values submited in the url. -->
<form action="submitpage.php" method="post">

    <!-- Remember the name choice as we use it later -->
    <select name="choice">
    
        <?php
            
            $available = 4;
            
            for ( $i = 0; $i <= 4; $i++ )
            {
            
        ?>
            <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
        
        <?php
        
            }
            
        ?>
    
    </select>
    
    <input type="submit" value="Submit" />

</form>

Below is the page which the form is directed to. It outputs the value of the select box.

submitpage.php

PHP
<?php

    // The option value from the select box is in the global variable $_POST.
    // The name of the field is the name of the index of the $_POST array.
    // Check if it 'isset' before continuing.
    $choice = isset( $_POST[ 'choice' ] ) ? $_POST[ 'choice' ] : 0 ;
    
    // Output the choice on the screen.
    echo 'Choice = ' . $choice;
    
    echo '<br /><br />';
    
    // It is worth taking a look at what is in the $_POST variable and have a look at all its variables.
    echo '<pre>'.print_r( $_POST , true ).'<pre>';

?>

Read the comments for more explanation.

 
Share this answer
 
Well first off, you have to enclose those select-lists in a form tag with its method set to POST or GET and the action set to the page it will submit to so that when you submit you can reference the product amounts via GET or POST. something like this:
PHP
<?php
mysql_connect('localhost');
mysql_select_db('db');
$sql = 'SELECT * FROM store';
$rs = mysql_query($sql);
echo '<table>';
while($row = mysql_fetch_array($rs)) 
{
   $p = $row['p'];
   $price = $row['price'];
   $total = $_POST[$row['p']] * $row['price'];
   echo '<tr>
      <td>Product:</td>
      <td>' . $p . '</td>
      </tr>
      <tr>
      <td>Unit Price:</td>
      <td>' . $price . '</td>
      </tr>
      <tr>
      <td>Quatity Choosen:</td>
      <td>' . $quantity . '</td>
      </tr>
      <tr>
      <td>Total Price:</td>
      <td>' . $total . '</td>
 
   </tr>';
}
echo '</table>';
mysql_close();
?>

Hope this helps.
 
Share this answer
 
v3

yes , thanx very much for your help

I as mentioned i have a database, what you described for me is not for a database

I'm not sure if i explain my problem exactly

Product ID  Quantity  Price

1              <select>    20 $

2              <select>    30$

there are - lets say 12 rows of products, i want when the user select for product1 12 items with 10$ per each (120$), and if he choose for product2 from the select option 5 items (150$), the problem am facing is in storing the selection of the user in the database, and i could'nt find a simple way to retrive them back after storing

I hope that my idea is clear for you now, and im very thankful for your help

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900