php - 从多个复选框和文本字段插入值

标签 php mysql checkbox insert textfield

我是 PHP 的初学者。我遇到了一个问题。我的想法是,我必须将 Actor 分配给选定的电影,并为每个 Actor 添加一个角色。我需要从列表中选择几个值,并通过文本字段为每个值添加描述。我的代码将所有检查的值添加到数据库中,但它使文本字段中的值变得困惑,检查的值与描述不匹配。我将非常感谢您的帮助! 我的代码: 形式:

<?php
$sqlquery = "SELECT artistId, firstname, lastname from $artists order by 2";
$result = mysqli_query($connect, $sqlquery);
if($result) {
    echo "<table class=\"addactor\">";
    echo "<tr>
        <td id=\"text\" colspan=\"2\"><h3>Assign an actor to the movie</h3></td>
    </tr>";
    while($sqlRow = mysqli_fetch_array($result, MYSQL_ASSOC)) {
        echo "<tr>";
        echo "<td>";
        echo "<input type=\"checkbox\" name=\"checkbox[]\" value=\"" . $sqlRow['artistId'] . "\"/> " . $sqlRow['firstname'] . " " . $sqlRow['lastname'] . "</td><td><input type=\"text\" name=\"textbox[]\"/></td>";
        echo "</tr>";

    }
    echo "<tr><td align=\"right\"><input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Add\"></td><td><input type=\"reset\" name=\"reset\" id=\"reset\" value=\"Reset\"></td></tr></table>;";

}
print '</table>';

与数据库的连接位于另一个文件中,该文件包含在此处。

第二部分:

if($_POST) {
        $checkbox = $_POST['checkbox'];
        $txt = $_POST['textbox'];
        $len = sizeof($checkbox);
        for($i = 0; $i < $len; $i++) {
            $sqlqr = "INSERT INTO $role (artistId, movieCode, Description) VALUES ('" . $checkbox[$i] . "', '" . $_POST['moviecode'] . "', '" . $txt[$i] . "')";
            mysqli_query($connect, $sqlqr);
        }
        $query = "INSERT INTO $movies(movieCode, title, dateOfIssue,category, description, image) VALUES ('" . $_POST['moviecode'] . "', '" . $_POST['title'] . "', '" . $_POST['dateofissue'] . "','" . $_POST['category'] . "', '" . $_POST['desc'] . "', '" . $_POST['image1'] . "')";
        mysqli_query($connect, $query);
        if(mysqli_query($connect, $query) || mysqli_query($connect, $sqlqr)) {
            echo "<h4>1 record added</h4>";

        }
        else {
            die('Error: ' . mysqli_error($connect));
        }
        print '</form>';
    }

最佳答案

未选中的值不会提交,并且 checkbox 数量与 textbox 数量不同。 您应该为输入名称数组提供相同的键:

$i = 0;
while($sqlRow = mysqli_fetch_array($result, MYSQL_ASSOC)) {
    echo "<tr>";
    echo "<td>";
    echo "<input type=\"checkbox\" name=\"checkbox[".$i."]\" value=\"" . $sqlRow['artistId'] . "\"/> " . $sqlRow['firstname'] . " " . $sqlRow['lastname'] . "</td><td><input type=\"text\" name=\"textbox[".$i."]\"/></td>";
    echo "</tr>";
    $i++;
}

也使用此代码:

$checkbox = $_POST['checkbox'];
$txt = $_POST['textbox'];
foreach ($checkbox as $key => $value)
    $sqlqr = "INSERT INTO $role (artistId, movieCode, Description) VALUES ('" . $value . "', '" . $_POST['moviecode'] . "', '" . $txt[$key] . "')";
    mysqli_query($connect, $sqlqr);
}

关于php - 从多个复选框和文本字段插入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20901262/

相关文章:

php - 在 zend 中在哪里保存自定义自动加载器?

php - PHP 中的非 ASCII 字符?

java - 如何在 Android 的换行符处切断(删除)复选框文本?

javascript - 使用PHP接口(interface)修改完整的MySQL表数据

mysql - 如何合并innoDB MySQL数据库中的2条记录

mysql - SQL,如何从 mysql 中选择一个包含 "/"的列

mysql - 选择分组到特定标识符的值

html - ionicons 用图标替换复选框和单选按钮

c# - 将复选框列表绑定(bind)到数据模型中的字典

php - 如何在月份日期条件下从mysql表中获取确切数据