PHP创建下拉菜单,插入数据库

时间:2017-12-05 00:17:44

标签: php html mysql mysqli

我正在尝试创建一个下拉菜单,供用户将他们的订单插入我的数据库系统。

我在创建下拉菜单时遇到了问题,但我对我应该做的事情有了一般的了解。我是新手,所以请耐心等待。 如果它有帮助,这是我的uml:enter image description here

<?php

$databaseName = 'pizza_db';
$databaseUser = 'root';
$databasePassword = 'root';
$databaseHost = '127.0.0.1';
$conn = new mysqli($databaseHost, $databaseUser, $databasePassword, $databaseName);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected sucessfully";
if(isset($_POST['submit'})){

$sql = "SELECT size FROM size";
$result = mysqli_query($sql); 
$opt = "<select name='size'>";
while($row = mysqli_fetch_assoc($result)) {
    $opt .= "option value='{$row['size']}' >{$row['size']}></option>";
}

$opt .="</select>";

if ($conn->query($sql)){
    $msg = "Data inserted successfully";
} else{
    $msg = "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>


<!DOCTYPE html>
<html>
<head>
    <title>Pizza</title>
</head>
<body>
<form action="size.php" method="POST">
    <select name="size">

    </select>
    <button type ="submit" name="submit"> Submit</button
</form>
</body>
</html>

我知道我的HTML代码错误或不完整。如何创建多个下拉菜单以及如何在一个脚本中使用多个$ sql insert语句以将数据提交到我的数据库中?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

  1. 你几乎没有错别字。在第13行中,if(isset($_POST['submit'})){应为if(isset($_POST['submit'])){。 在while循环中,你应该有$opt .= "<option value='{$row['size']}' >{$row['size']}></option>";(缺少“&lt;”)

  2. 此外,还有一些冗余代码。 mysqli_query()$conn->query()执行相同的操作(see this SO tread)。简单的OOP与程序语法。

  3. 您可以删除

    if ($conn->query($sql)){
        $msg = "Data inserted successfully";
    }
    else{
         $msg = "Error: " . $sql . "<br>" . $conn->error;
    }
    

    之后,这样的事情就足够了

    <?php
    
    $databaseName = 'pizza_db';
    $databaseUser = 'root';
    $databasePassword = 'root';
    $databaseHost = '127.0.0.1';
    $conn = new mysqli($databaseHost, $databaseUser, $databasePassword, $databaseName);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected sucessfully";
    if(isset($_POST['submit'])){
    
        $sql = "SELECT size FROM size";
        $result = mysqli_query($sql); 
        $opt = "<select name='size'>";
        while($row = mysqli_fetch_assoc($result)) {
            $opt .= "<option value='{$row['size']}' >{$row['size']}></option>";
        }
    
        $opt .="</select>";
        $conn->close();
    }
    ?>
    

    但是,正如我在此理解的那样,您有一个空表单,在提交时会获得所有大小。如果你想让它像 php get sizes &gt; 生成表单&gt; 提交表单并重定向到下一步你应该像这样或类似的那样(并从php部分删除if $_POST['submit']条件)

    <!DOCTYPE html>
    <html>
    <head>
        <title>Pizza</title>
    </head>
    <body>
    <form action="submit-chosen-size.php" method="POST">
        <label for="size">Select your size:</label>
        <?php echo($obj); ?>
        <button type ="submit" name="submit"> Submit</button
    </form>
    </body>
    </html>
    

    编辑:请尝试使用此代码。它与上面的代码非常相似,但有助于找到问题

    <?php
    $databaseName = 'pizza_db';
    $databaseUser = 'root';
    $databasePassword = 'root';
    $databaseHost = '127.0.0.1';
    $conn = new mysqli($databaseHost, $databaseUser, $databasePassword, $databaseName);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected sucessfully";
    
    $sql = "SELECT size FROM size";
    $result = mysqli_query($sql); 
    $opt = "<select name='size'>";
    while($row = mysqli_fetch_assoc($result)) {
        $opt .= "<option value='{$row['size']}' >{$row['size']}></option>";
        echo $row['size']; // you can remove this later
    }
    $opt .="</select>";
    echo $obj; //you can remove this later
    $conn->close();
    ?>
    
    <!DOCTYPE html>
    <html>
        <head>
            <title>Pizza</title>
        </head>
        <body>
            <form action="submit-chosen-size.php" method="POST">
                <label for="size">Select your size:</label>
                <?php echo(isset($obj) ? $obj : "Empty"); ?>
                <button type ="submit" name="submit"> Submit</button
            </form>
        </body>
    </html>