Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP从多个复选框插入数组并将文本输入mySQL_Php_Mysql_Checkbox - Fatal编程技术网

PHP从多个复选框插入数组并将文本输入mySQL

PHP从多个复选框插入数组并将文本输入mySQL,php,mysql,checkbox,Php,Mysql,Checkbox,我在尝试插入来自php中动态生成的表单的信息时遇到了一个问题。 表单由可变数量的输入组成,所有输入均由四个输入元素组成。请参见下面的“我的表单”生成方式: <?php $result = mysql_query("SELECT id,name,description FROM todo_q WHERE todo_id = $todo_id AND active = 'y'"); while($todo_q=mysql_fetch_array($result)

我在尝试插入来自php中动态生成的表单的信息时遇到了一个问题。 表单由可变数量的输入组成,所有输入均由四个输入元素组成。请参见下面的“我的表单”生成方式:

<?php $result = mysql_query("SELECT id,name,description FROM todo_q WHERE todo_id = $todo_id AND active = 'y'");
                while($todo_q=mysql_fetch_array($result)){

                                                echo '<label>';
                                                echo $todo_q['name'];
                                                echo '</label><br>'; 

                                                echo '<input type="checkbox" name="value[]" value="y" />';
                                                //echo '<input type="hidden" name="value[]" value="n" />';

                                                echo '<label>';
                                                echo $todo_q['description'];
                                                echo '</label><br>';
                                                echo '<input type="text" id="comment" name="comment[]">';

                                                echo '<input type="hidden" name="user_id[]" value="';
                                                echo $user_id;
                                                echo '" />';
                                                echo '<input type="hidden" name="todo_id[]" value="';
                                                echo $todo_q['id'];
                                                echo '" />';
                                                echo '<HR>';

                                                }?> 

您需要将索引添加到输入复选框中,并注释名称,如:

$cbIndex = 0;
while($todo_q=mysql_fetch_array($result)){  
    echo '<label>';
    echo $todo_q['name'];
    echo '</label><br>'; 
    // Generate checkbox with index of current result
    echo '<input type="checkbox" name="value[' . $cbIndex . ']" value="y" />';
    // Generate comment with index of current result
    echo '<input type="text" id="comment" name="comment[' . $cbIndex . ']">';
    echo '<input type="hidden" name="user_id[' . $cbIndex . ']" value="';
    echo $user_id;
    echo '" />';
    echo '<input type="hidden" name="todo_id[' . $cbIndex . ']" value="';
    echo $todo_q['id'];
    echo '" />';
    echo '<HR>';
    // Inc of index
    $cbIndex++;
}
顺便说一句,您不需要存储复选框的值,它将始终为“y”

INFO对于测试应用程序来说,这很好,但正如@Pogrindis和@johnconde所评论的,这是不安全的代码。MySQLi/PDO+prepare语句将避免SQL注入


强制性警告:请不要在生产中使用此代码。。。清理查询以防止出现某些XSS。。你也检查过这个相关问题吗?供参考。它们不再得到维护。看到了吗?相反,请了解,并使用或-将帮助您决定哪一个最适合您。您的脚本有可能查看使用时发生的情况。谢谢@camile的建议。它似乎起作用了。我不会像在“实时”应用程序中那样使用它。现在这只是个测试,太好了!很高兴能帮助你
$cbIndex = 0;
while($todo_q=mysql_fetch_array($result)){  
    echo '<label>';
    echo $todo_q['name'];
    echo '</label><br>'; 
    // Generate checkbox with index of current result
    echo '<input type="checkbox" name="value[' . $cbIndex . ']" value="y" />';
    // Generate comment with index of current result
    echo '<input type="text" id="comment" name="comment[' . $cbIndex . ']">';
    echo '<input type="hidden" name="user_id[' . $cbIndex . ']" value="';
    echo $user_id;
    echo '" />';
    echo '<input type="hidden" name="todo_id[' . $cbIndex . ']" value="';
    echo $todo_q['id'];
    echo '" />';
    echo '<HR>';
    // Inc of index
    $cbIndex++;
}
foreach ($_POST["value"] as $cbIndex => $cbValue) {
    $query_parts[] = "('" . $_POST['value'][$cbIndex] . "','" . $_POST['comment'][$cbIndex] . "'," . $_POST['user_id'][$cbIndex] . "," . $_POST['todo_id'][$cbIndex] . ")";
    // or
    $query_parts[] = "('" . $cbValue . "','" . $_POST['comment'][$cbIndex] . "'," . $_POST['user_id'][$cbIndex] . "," . $_POST['todo_id'][$cbIndex] . ")";
}
...