PHP/Mysql <select> problem

Discussion in 'PHP' started by Nokia N93, Nov 21, 2009.

  1. #1
    PHP_Mysql <select> problem?


    Hi all, i'm new to PHP and i don't know if i can ask questions here or not

    but i have a little store
    i want in the databse to store the avilable amount of a product, and list it in a <select> statemnet, when the user choose from the select list , the number is stored in the databse so that i can retrieve it later.

    i want this process to repeat for all the products i have

    i started a code , but i couldn't know how to store the selection that the user choose


    
    
    <?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];
    $av = $row[av];
    $price = $row[price];
    
    echo "<tr>
    
    <td>$p</td>
    <td><select name=".$choice.">";
    for($i=0; $i<=$avilable; $i++)
    {
    echo "<option value=".$i." name=".$i.">$i</option>";
    } 
    echo "<td >$price</td> 
    </tr>";
    }
    mysql_close();
    ?>
    
    Code (markup):
    after that when i click submit i want a page that prints out for every product the amount that the user choose + the price and the total price

    i hope you can help me
     
    Nokia N93, Nov 21, 2009 IP
  2. Nokia N93

    Nokia N93 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Any help
    pllllllllllllllllllllllllllllz
     
    Nokia N93, Nov 28, 2009 IP
  3. inanobot

    inanobot Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Do you mean to select multiple item and insert them in to SQL database? Take a look at this example:

    HTML
    PHP
     
    Last edited: Nov 28, 2009
    inanobot, Nov 28, 2009 IP
  4. Nokia N93

    Nokia N93 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    inanobot

    thanx 4 ur reply

    i have multiple <select> statements
    I have a homework for calculating the invoice of multiple products
    the quantity of the products is stored in a DB and i retrieve them from the DB to the select statement

    product1: <select>(select from 0 untill the quantity stored in DB): price
    product2: <select>: price
    product3: <select>: price
    ...
    product n: <select>: price

    So, i have a problem when i hi the submit button, i don't know how to store what the user choose from every select and how to store it in the Database
     
    Nokia N93, Nov 28, 2009 IP
  5. jpinheiro

    jpinheiro Peon

    Messages:
    1,211
    Likes Received:
    15
    Best Answers:
    1
    Trophy Points:
    0
    #5
    is this what you wanted?

    
    <?php
    mysql_connect("localhost", "username", "password") or die(mysql_error());
    mysql_select_db("db") or die(mysql_error());
    
    if(isset($_POST['submit'])) {
    
    $choice = $_POST['choice'];
    
    mysql_query("INSERT INTO tablename 
    (choice) VALUES('$choice') ") 
    or die(mysql_error()); 
    
    }
    
    $query = "SELECT * FROM store";
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result))  {
    $p = $row[p];
    $av = $row[av];
    $price = $row[price];
    ?>
    <form action="" method="post">
    <?php
    echo "<tr>
    
    <td>$p</td>
    <td><select name='$choice'>";
    for($i=0; $i<=$avilable; $i++)
    {
    echo "<option value='$i' >$i</option>";
    } 
    echo "<td >$price</td> 
    </tr>";
    }
    ?>
    <input type="submit" value="Submit" name="submit">
    
    PHP:
    -John
     
    jpinheiro, Nov 28, 2009 IP
  6. Nokia N93

    Nokia N93 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6

    thanx John for ur help
    but it didn't work for me

    Should i put isset($_POST['submit']) here or in the page that i specify in the form ??

    Sorry again
     
    Nokia N93, Dec 2, 2009 IP
  7. jpinheiro

    jpinheiro Peon

    Messages:
    1,211
    Likes Received:
    15
    Best Answers:
    1
    Trophy Points:
    0
    #7
    well its all processed on one page with the form

    -John
     
    jpinheiro, Dec 3, 2009 IP