Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 我能';t将字符串值传递到数组中_Php_Arrays - Fatal编程技术网

Php 我能';t将字符串值传递到数组中

Php 我能';t将字符串值传递到数组中,php,arrays,Php,Arrays,我有这段代码,但无法传递数组变量$component的值,该变量的输入数据为string。我该怎么办 <?php if(isset($_POST['submit_tbl_grade'])){ $id = isset($_POST['id']) ? $_POST['id'] : array(); $component = $_POST['component'];

我有这段代码,但无法传递数组变量$component的值,该变量的输入数据为string。我该怎么办

<?php if(isset($_POST['submit_tbl_grade'])){
                    $id = isset($_POST['id']) ? $_POST['id'] : array();
                        $component = $_POST['component'];
                        $sub_component_number = $_POST['sub_component_number'];
                        $subject = $_POST['subject'];
                        $score = $_POST['score'];

                    for($i = 0 ; $i < count($id) ; $i++){
                        $record = "INSERT INTO tbl_grade(stud_id,subject_id,component,sub_component_number,component_value) VALUES ($id[$i],$subject[$i],$component[$i],$sub_component_number[$i],$score[$i])";
                        mysqli_query($db,$record);
                    }
                   } ?>

请先阅读并检查$\u POST数据,以确保您了解数据结构

示例代码中还有两个问题:

  • 过滤输入和转义输出

    必须转义输入数据以防止SQL注入攻击。 查看@zerkms评论的答案

  • 插入值的格式必须正确

    例如,如果
    tbl_grade.component
    定义为“varchar”,则必须用引号括起值,请检查以下示例

    INSERT INTO `tbl_grade` ( `component` ) VALUES ("hello world");
    

  • 检查表单html是什么样子的?