-1

我有个问题。我正在使用一个while循环。我在查询 $rows['price'] 中放置了一个。如何将循环内的整个插入值提交到表中?它有 5 个结果。

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>

输出将是这样的。

价格:__ % 价格:__ % 价格:__ % 价格:__ % 价格:__ % |提交|

page2.php

             <?php


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

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


         ?>

不知何故,它工作了一点。我以 90,80,70,60,50 的形式输入值。但唯一插入表中的是最后一个,位于底部 (50)。其余的不要。

4

1 回答 1

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.

于 2013-04-30T06:32:00.210 回答