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 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决