douwu0882 2018-04-27 20:40
浏览 62
已采纳

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

Through hours of research and looking through code in questions submitted on this site, I was finally able to get the select options (dropdowns) to pull data from my database tables into the dropdown lists on my html form. However, my issue is that when the fields on the form were inputs they inserted the new information into the database just fine. Unfortunately, now that I've implemented the dropdown lists as part of the form, none of the information from the form inserts into the database anymore. Clicking on the 'submit' button returns the response that it was successful, but when I check the table in the database, the new information is not there. I'm sorry I haven't been able to figure this piece of functionality out by myself. I noticed my last question received negative feedback, so I'm leary to even submit this one, but I really need some help. Will you please look through the following code and let me know what I'm missing or have coded incorrectly? I just need to know what I need to do to make the selected values from the dropdown lists insert into the 'dvd' table and 'categoryname' and 'genretype' fields, respectively.

<?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");
?>
  • 写回答

1条回答 默认 最新

  • dongyin2885 2018-04-27 20:47
    关注

    You forgot to actually run the insert into database

    $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.
    ....
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题