HTML表多个数据保存在mysql数据中

时间:2017-10-16 06:32:04

标签: php html mysqli

我已经创建了php程序来订购食品。用户已获得表格。该html表有4列food itemquantitypriceorder。用户只能向order列提供输入。

当用户在HTML表格中输入输入并单击订单按钮时​​,应将其保存在MySQL订单表中。但我无法成功地完成这项工作。我想只使用PHP来做这件事。不使用JavaScript。帮我解决这个问题。

HTML table interface

HTML代码

<form action="order2.php" method="POST">
    <table border="1">
        <tr>
            <th>Food Item</th>
            <th>Quantity available</th> 
            <th>Item Price</th>
            <th>Order</th>  
        </tr>
        <?php
        while ($row = mysqli_fetch_assoc($result1)) {
            ?>
            <tr>
                <td><?php echo $row['fitem']; ?></td>
                <td><?php echo $row['qty']; ?></td>
                <td><?php echo $row['price']; ?></td>
                <td><input type="text" name="order"></td>
            </tr>
            <?php
        }
        ?>
        <tr>
            <td>
                <input type="submit" value="order" align="right">
            </td>
        </tr>
    </table>
</form>

php code

<?php
session_start();

$order = $_POST['order'];

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mid";

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
} else {
    while ($row = mysqli_fetch_assoc($result1)) {
        $sql = "insert into order (fname,order) values('$row['fitem']',$order)";

        if (mysqli_query($conn, $sql)) {
            echo "New order created successfully";
        } else {
            echo mysqli_error($conn);
        }
    }
}
?>

0 个答案:

没有答案