从多个复选框和文本字段插入值 [英] Inserting values from multiple checkboxes and textfields

查看:129
本文介绍了从多个复选框和文本字段插入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的初学者。我遇到了问题。想法是,我必须分配演员到一个选定的电影,并为每个添加一个角色。我需要从列表中选择几个值,并为每个通过texfields添加一个描述。我的代码将所有选中的值添加到数据库,但它使得textfields的值混乱,检查的值与描述不匹配。我真的会感谢你的帮助!
我的代码:
Form:

I am a beginner in PHP.I am stuck with a problem. The idea is that I have to assign actors to a selected movie and add a role for each. I need to pick several values from the list and add a description for each via texfields. My code adds all the checked values to the database, but it makes a mess with the values from the textfields, the checked values don't match with the description. I would be really grateful for your help! My code: Form:

<?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>';

与数据库的连接位于另一个文件中,

The connection to the database is in another file, which is included here.

第二部分:

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>';
    }


推荐答案

未提交未选中的值复选框数量与文本框不同。
您应该给输入名称数组相同的键:

Unchecked values are not submitted and checkbox quantity not same with textbox. You should give input name array same keys :

$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);
}

这篇关于从多个复选框和文本字段插入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆