【发布时间】:2016-05-27 15:22:19
【问题描述】:
我有一个表单,允许用户输入多个学生的分数,并指定记录分数的测试(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 的回显中得到以下输出
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 on line ---- $score = mysqli_real_escape_string($connection, $score[$i]);
为什么会发生这种情况,我该如何解决?
【问题讨论】: