【问题标题】:Select multiple is not putting any values in the database or inserts only the last chosen选择多个没有将任何值放入数据库中或仅插入最后选择的值
【发布时间】:2018-04-30 12:58:38
【问题描述】:

我正在尝试让一个多选(下拉)列表接受多个值并将它们插入到 mysql 数据库表中的一个字段中,但由于某种原因这些值没有被放入该字段中。我已经研究了几个小时,但其他问题都与我的情况不完全相关,因为我在 mysql 数据库中的另一个表中动态提取值。现在的代码方式,我没有收到任何错误,但没有任何东西放入数据库。

我尝试过使用诸如$_GETforeachif elseimplode 等语句,但我一定不能正确使用语法,因为似乎没有任何效果。我也尝试将[ ] 括号从:name='genretype[]$genretype = mysqli_real_escape_string($dbc, $_POST['genretype[]']); 行中取出,但是当我这样做时,我收到了这个错误:

mysqli_real_escape_string() 期望参数 2 是字符串,给定数组

有人可以看看这个,如果你发现我做错了什么,请告诉我? (为了您的方便,我只包含了与我的问题直接相关的代码。我的另一个下拉列表没有选择多个,效果很好。)

php代码的顶部:

// 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.
            $result = mysqli_query($dbc, $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.
            }

形成部分代码:

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

            echo 'Genre (select all that apply): <select type="text" class="dropdown" name="genretype[]" multiple="multiple" size="5">';
            while($ddlrow2=mysqli_fetch_array($ddlresult2, MYSQLI_ASSOC)){
            echo "<option value='".$ddlrow2['genretype']."'>" . $ddlrow2['genretype'] . "</option>";
        }
            echo "</select>";
        ?>

【问题讨论】:

  • 谢谢,柯克。我不知道我能做到这一点。感谢您的建议。

标签: php mysql html select dropdown


【解决方案1】:

我的问题没有得到任何答复,但我能够通过反复试验自己弄清楚,所以我想我会分享解决问题的方法。

我只需要将代码更改为以下内容。

php 代码的顶部(我更改了$genretype = 之后的行,并删除了该行末尾'genretype' 之后的括号)。

新的“工作”代码:

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

构成部分代码(连接和查询行后面的大部分我都改了):

<?php
    $ddlquery2 = "SELECT genretype FROM genre ORDER BY genretype ASC";
    $ddlresult2 = mysqli_query($dbc, $ddlquery2) or die("Bad SQL: $ddlquery2");
    $match = "SELECT genretype FROM dvd WHERE id=$id";

    $numOpts = count($match);
    if ($numOpts>1){
    $matches=implode('","',$match);
    }elseif ($numOpts==1){
    $matches=$match;
    }else{
    $matches="";
    }

    echo 'Genre (select all that apply): <select name="genretype[]" multiple="multiple" size="5" class="multilist"';
    while($ddlrow2=mysqli_fetch_array($ddlresult2, MYSQLI_ASSOC)){
    echo "<option value='".$ddlrow2['genretype']."'>" . $ddlrow2['genretype'] . "</option>";
    }   
    echo "</select>";
?>

【讨论】:

    猜你喜欢
    • 2020-03-02
    • 2016-07-08
    • 2019-05-04
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    相关资源
    最近更新 更多