更新时未发送表单字段值

时间:2016-05-27 15:22:20

标签: php mysql

我有一个表单,允许用户为多个学生输入分数,并指定要记录分数的测试(test1,test2或test3)。我的查询的回显显示只有第一行数据被发送到sql update语句,而其他数据似乎被截断(完全切断。)

这是我的代码:

<?php
if (isset($_POST['submit'])) {

    # process the form
    $student_id = $_POST["student_id"];
    $subject_id = $result['subject_id'];
    $type = $_POST["type"];
    $score = $_POST["score"];

    for($i=0; $i < count($student_id); $i++) {
        $studentid = mysqli_real_escape_string($connection, $student_id[$i]);
        $subjectid = mysqli_real_escape_string($connection, $subject_id);
        $type = mysqli_real_escape_string($connection, $type);
        $score = mysqli_real_escape_string($connection, $score[$i]);

        $query = "UPDATE records SET $type='{$score}' WHERE student_id={$studentid} AND subject_id={$subjectid}";
        //$result = mysqli_query($connection, $query);
        echo $query;
    }
}
?>

例如,如果将值10,11和12输入到表单中,我将从$ query的echo中获得以下输出

UPDATE records SET test1=' 10' WHERE student_id=53 AND subject_id=2
UPDATE records SET test1='1' WHERE student_id=54 AND subject_id=2
UPDATE records SET test1='' WHERE student_id=55 AND subject_id=2

以及以下错误

  

注意:未初始化的字符串偏移量:2在线---- $ score = mysqli_real_escape_string($ connection,$ score [$ i]);

为什么会发生这种情况?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

你覆盖了数组$得分。它看起来像这样:

$scoreOther = mysqli_real_escape_string($connection, $score[$i]);

并将$ scoreOther放入查询而不是$ score然后