-1

I have a problem. Im using a while loop. I placed a inside the query $rows['price']. How do I submit the entire inserted values inside the loop to a table?. It has 5 results.

page1.php

        <?php

    $sql = mysql_query("SELECT id FROM table where id = '$ID'");

        while($row = mysql_fetch_assoc($sql)){

        echo '<form action = "page2.php" name = "add" method = "post">';
        echo "<b>Price:</b> ";
        echo '<input size = "1" type="text" name="price" value = ""/>';
        echo "%";
        echo "<br/>";

        }

        ?>

    <input type="submit" name="price" value="SUBMIT"/>
    </form>

The output would be like this.

Price: __ % Price: __ % Price: __ % Price: __ % Price: __ % |SUBMIT|

page2.php

             <?php


    if(isset($_POST['submit'])){
        $price = $_POST['price'];

        mysql_query("INSERT INTO items_tbl(price) VALUES('$price');


         ?>

Somehow it worked a little. I inputed values in the form from 90,80,70,60,50. But the only one inserted in the table is the last, at the bottom (50). The rest dont.

raziel2101
  • 13
  • 3
  • 8
  • 2
    [`MySQL`](http://php.net/manual/en/book.mysql.php) (_mysql_*_ functions) extension is [***deprecated***](http://php.net/manual/en/function.mysql-connect.php). I suggest to use [`MySQLi`](http://php.net/manual/en/book.mysqli.php) (_mysqli_*_ functions) or [`PDO`](http://php.net/manual/en/book.pdo.php) instead. – BlitZ Apr 30 '13 at 06:30
  • I know, im still learning the basic stuff in php. Im planning the use mysqli anyways after ive learned them enough – raziel2101 Apr 30 '13 at 06:37

1 Answers1

0

Use it like this, the <form> tag should be outside while loop and you have to put $row['price'] inside value

$sql = mysql_query("SELECT price FROM table where id = '$ID'");

echo '<form action = "page2.php" name = "add" method = "post">';
while($row = mysql_fetch_assoc($sql)){
    echo "<b>Price:</b> ";
    echo '<input size = "1" type="text" name="weight'.$row['price'].'" value = ""/>';
    echo "%";
    echo "<br/>";

}

?>
<input type="submit" name="submit" value="SUBMIT"/>
</form>

In page2.php use this

$val = implode(",", $_POST);
mysql_query("INSERT INTO items_tbl(price) VALUES('$val');

Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100