代码1完美无缺,并更新了考勤表:
<?php
$date = $_POST['date'];
$subject = $_POST['subject'];
$con=mysqli_connect("localhost","root","","notifier");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM student");
echo "Enter the attendance. Please untick for 'ABSENT' students and submit";
echo "<br>";
echo "<form action=\"d.php\" method=\"post\">";
while($row = mysqli_fetch_array($result))
{
echo "<br>" .$row['classrollno'] . "   <input type=\"checkbox\" name=\"p[]\" value=\"" . $row['studentid'] . "\" checked>";
$sql="INSERT INTO attendance (rollno, subjectid, date)
VALUES
('{$row['classrollno']}','$_POST[subject]','$_POST[date]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
}
mysqli_close($con);
echo "<input type=\"hidden\" name=\"date\" value=\"" . $date . "\">";
echo "<input type=\"hidden\" name=\"subject\" value=\"" . $subject . "\">";
echo "<input type=\"submit\" name=\"submit\" value=\"submit\">";
echo "</form>";
?>
代码二给出错误,应根据从上一页收到的复选框更新表:
<?php
$con=mysqli_connect("localhost","root","","notifier");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
foreach($_POST['p'] as $value)
{
$sql="UPDATE attendance SET presenty='P'
WHERE studentid='{$value}' AND subjectid='$_POST[subject]' AND date='$_POST[date]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
}
mysqli_close($con);
?>
错误:您的SQL语法出错;查看与您的MySQL服务器版本对应的手册,以获得正确的语法,以便在附近使用&#39;)&#39;在第2行
但这行在以前的文件中完全正常。
答案 0 :(得分:1)
Line 1: UPDATE attendance SET presenty='P'
Line 2: WHERE studentid='foo' AND subjectid='bar' AND date='baz')
错误:......在第2行附近')'
第2行的唯一)
是结束的{{1}}。删除它。
+
答案 1 :(得分:-1)
你缺少大括号
$sql="UPDATE attendance SET presenty='P'
WHERE studentid='{$value}' AND subjectid='$_POST[subject]' AND date='$_POST[date]')";
必须是
$sql="UPDATE attendance SET presenty='P'
WHERE studentid='{$value}' AND subjectid='{$_POST[subject]}' AND date='{$_POST[date]}'";
// ^ ^ ^ ^
警告:您的代码很容易被SQL注入。请尝试使用预准备语句或至少验证并正确转义$_POST
变量
编辑: Yuor第一个代码也有类似的问题
('{$row['classrollno']}','$_POST[subject]','$_POST[date]')";
应该是
('{$row['classrollno']}','{$_POST[subject]}','{$_POST[date]}')";
参考:PHP Strings Manual