PHP代码使用文本框输入插入sql db,但不使用选择选项(下拉列表)

PHP代码使用文本框输入插入sql db,但不使用选择选项(下拉列表),php,html,mysqli,Php,Html,Mysqli,通过数小时的研究和查看该网站上提交的问题中的代码,我终于能够获得“选择选项”下拉列表,将数据库表中的数据拉入html表单上的下拉列表。 然而,我的问题是,当表单上的字段被输入时,它们会将新信息插入数据库。不幸的是,现在我已经实现了作为表单一部分的下拉列表,表单中的任何信息都不再插入数据库。单击“提交”按钮返回成功的响应,但当我检查数据库中的表时,新信息不在那里。 很抱歉,我自己还没能弄明白这一功能。我注意到我的上一个问题收到了负面反馈,所以我很想提交这个问题,但我真的需要一些帮助。 请您检查一下

通过数小时的研究和查看该网站上提交的问题中的代码,我终于能够获得“选择选项”下拉列表,将数据库表中的数据拉入html表单上的下拉列表。 然而,我的问题是,当表单上的字段被输入时,它们会将新信息插入数据库。不幸的是,现在我已经实现了作为表单一部分的下拉列表,表单中的任何信息都不再插入数据库。单击“提交”按钮返回成功的响应,但当我检查数据库中的表时,新信息不在那里。 很抱歉,我自己还没能弄明白这一功能。我注意到我的上一个问题收到了负面反馈,所以我很想提交这个问题,但我真的需要一些帮助。 请您检查一下下面的代码,让我知道我遗漏了什么或代码有误吗?我只需要知道我需要做些什么,才能使从下拉列表中选择的值分别插入到“dvd”表和“categoryname”和“genretype”字段中

<?php 
session_start();
    //include the header
    include ('../main/header.php');
// Check if the form has been submitted.
if (isset($_POST['submitted'])) {
    require_once ('../../../mysqli_connect.php'); // Connect to the db.
    $errors = array(); // Initialize error array.

    // Check for a first name.
    if (empty($_POST['title'])) {
        $errors[] = 'You forgot to enter a title.';
    } else {
        $title = mysqli_real_escape_string($dbc, $_POST['title']);
    }

    // Check for a category.
    if (empty($_POST['numavail'])) {
        $errors[] = 'You forgot to enter quantity purchased.';
    } else {
        $numavail = mysqli_real_escape_string($dbc, $_POST['numavail']);
    }

    // Check for a category.
    if (empty($_POST['categoryname'])) {
        $errors[] = 'You forgot to enter a category.';
    } else {
        $categoryname = mysqli_real_escape_string($dbc, $_POST['categoryname']);
    }

    // Check for a genre.
    if (empty($_POST['genretype'])) {
        $errors[] = 'You forgot to enter a genre.';
    } else {
        $genretype = mysqli_real_escape_string($dbc, $_POST['genretype']);
    }

    if (empty($errors)) { // If everything's OK.
        // Add the movie to the database.
        // Check for existing record.
        $query = "SELECT id FROM dvd WHERE title='$title'";
        $result = mysqli_query($dbc, $query);
        if (mysqli_num_rows($result) == 0) { // if there is no such movie title
        $query = "INSERT INTO dvd (title, numavail, categoryname, genretype)
            VALUES ('$title', '$numavail', '$categoryname', '$genretype')";
            // Make the query.
            if ($result) { // If it ran OK.
                echo "<p><b>Success! The new movie has been added.</b></p>";
                echo ('<p><div style="margin-top:30px;">');
                echo ('<span style="float:left;">');
                echo ('<FORM METHOD="LINK" ACTION="../dvd/index.php"><INPUT TYPE="submit" VALUE="Back to DVDs" STYLE="margin:0px 15px 0px 0px;"></form></span></div></p>');
                echo ('<br style="clear:both;"></br>');

                exit();
            } else { // If it did not run OK.
                $errors[] = 'The movie could not be added due to a system error. We apologize for any inconvenience.'; // Public message.
                $errors[] = mysqli_error($dbc); // MySQL error message.
            }

        } else { // Title is already taken.
            $errors[] = 'The movie title entered already exists.';
        }

    } // End of if (empty($errors)) IF.

    mysqli_close($dbc); // Close the database connection.

} else { // Form has not been submitted.
    $errors = NULL;
} // End of the main Submit conditional.

// Begin the page now.
if (!empty($errors)) { // Print any error messages.
    echo '<p class="error">The following error(s) occurred:<br />';
    foreach ($errors as $msg) { // Print each error.
        echo "$msg<br />";
    }
    echo '</p>';
    echo '<p style="color:red; font-weight:bold;"><em>Please try again.</em></p></br>';
}

// Create the form.
?>
<h1>Add a Movie</h1>
<h2>Please complete all of the fields below:</h2>

    <form action="../dvd/add.php" method="post">

    <p>Title: <input type="text" name="title" size="15" maxlength="15" value="<?php echo $_POST['title']; ?>"></p>
    <p>Quantity Purchased: <input type="text" name="numavail" size="15" maxlength="30" value="<?php echo $_POST['numavail']; ?>"></p>
    <p>
        <?php
            include ('../../../mysqli_connect.php'); // Connect to the db.
            $ddlquery = "SELECT categoryname FROM category ORDER BY categoryname ASC";
            $ddlresult = mysqli_query($dbc, $ddlquery) or die("Bad SQL: $ddlquery");

            echo 'Category: <select name="categoryname" size="1">';
            while($ddlrow=mysqli_fetch_array($ddlresult, MYSQLI_ASSOC)){
            echo "<option value='".$ddlrow['categoryname']."'>" . $ddlrow['categoryname'] . "</option>";
        }
            echo "</select>";
        ?>

    <p>
    <?php
            $ddlquery2 = "SELECT genretype FROM genre ORDER BY genretype ASC";
            $ddlresult2 = mysqli_query($dbc, $ddlquery2) or die("Bad SQL: $ddlquery");

            echo 'Genre: <select name="genretype" size="1">';
            while($ddlrow2=mysqli_fetch_array($ddlresult2, MYSQLI_ASSOC)){
            echo "<option value='".$ddlrow2['genretype']."'>" . $ddlrow2['genretype'] . "</option>";
        }
            echo "</select>";
        ?>
    <p>
    <input type="submit" name="submit" value="Submit">
    <input type=reset value=Reset>
    <input type="hidden" name="submitted" value="TRUE"></p>
    </form>


<?php
// Include footer.php
include("../../includes/footer.php");
?>

您忘记实际将insert运行到数据库中

$result = mysqli_query($dbc, $query);
if (mysqli_num_rows($result) == 0) { // if there is no such movie title
    $query = "INSERT INTO dvd (title, numavail, categoryname, genretype)
        VALUES ('$title', '$numavail', '$categoryname', '$genretype')";
    // Make the query.
    $result = mysqli_query($dbc, $query); // <---- ADD HERE
    if ($result) { // If it ran OK.
....

这正是我所缺少的。谢谢,谢谢,谢谢。你是我的英雄!如果你知道我花了多少时间来寻找这个问题的答案,只是因为我不想在这里发布另一个问题。然后你来了,非常和蔼,马上就回答了。但愿我有你的知识。更新了我对这个网站的看法!您能否将示例代码简化为一个不起作用的字段?这是一个最小的例子,以确保它没有失败的一个无关的原因。