不在mysql、php中更新
好的,我正在创建一个考勤系统,我想标记一个学生是否出席,这是我的代码不在mysql、php中更新,php,html,mysql,forms,Php,Html,Mysql,Forms,好的,我正在创建一个考勤系统,我想标记一个学生是否出席,这是我的代码 <?php if (isset($_POST['submit'])) { $present = $_POST['present']; } $test3= "SELECT * FROM course_attendance, students, courses, attendance WHERE course_attendance.course_id=courses.course_id AND course_att
<?php
if (isset($_POST['submit'])) {
$present = $_POST['present'];
}
$test3= "SELECT * FROM course_attendance, students, courses, attendance WHERE course_attendance.course_id=courses.course_id AND course_attendance.week_id=attendance.week_number_id AND course_attendance.student_id= students.student_id AND courses.course_id='101' AND attendance.week_number_id='2' ";
$result = mysql_query($test3) or die(mysql_error());
echo "</br><table border='1' align='center'><tr> <th><strong>Student ID</strong></th> <th><strong>First Name </strong></th> <th><strong>Last Name</strong></th> <th><strong>Present</strong></th> </tr> ";
while($rows=mysql_fetch_array($result)){
echo"<form name='Biology_lecture11.php' method='post'>";
echo "<tr><td width='100' align='center'>" .$rows['student_id'].
"</td><td width='120' align='center'>" .$rows['fname'].
"</td><td width='120' align='center'>" .$rows['lname'].
"</td><td><input type='text' name='present' value=" .$rows['present'] . ">";
}
echo "</table>";
?>
<input type='submit' name='Submit' value='Submit' >
</form>
<?php
$sql="UPDATE course_attendance SET present='$present' WHERE course_id='101' AND week_id='2' ";
$result=mysql_query($sql);
if($result){
echo "Successfully logged the attendance";
}
else {
echo"ERROR";
}
?>
问题是,它不会更新数据库中的present字段,任何人都知道有什么问题您在表标记和while循环中使用了一个表单,这将不起作用,下面是正确的代码
<?php
if (isset($_POST['submit'])) {
$present = $_POST['present'];
$sql="UPDATE course_attendance SET present='$present' WHERE course_id='101' AND week_id='2' ";
$result=mysql_query($sql);
if($result) {
echo "Successfully logged the attendance";
}
else {
echo"ERROR";
}
}
?>
<form name='Biology_lecture11.php' method='post'>
<table border="1" align="center">
<tr>
<th><strong>Student ID</strong></th>
<th><strong>First Name </strong></th>
<th><strong>Last Name</strong></th>
<th><strong>Present</strong></th>
</tr>
<?php
$test3= "SELECT * FROM course_attendance, students, courses, attendance WHERE course_attendance.course_id=courses.course_id AND course_attendance.week_id=attendance.week_number_id AND course_attendance.student_id= students.student_id AND courses.course_id='101' AND attendance.week_number_id='2' ";
$result = mysql_query($test3) or die(mysql_error());
while($rows=mysql_fetch_array($result)) {
echo "<tr><td width='100' align='center'>" .$rows['student_id']."</td>
<td width='120' align='center'>" .$rows['fname']."</td>
<td width='120' align='center'>" .$rows['lname']."</td>
<td><input type='text' name='present' value=" .$rows['present']."></td></tr>";
}
echo "</table>";
?>
<input type='submit' name='Submit' value='Submit' >
</form>
我看到的一个错误是,你这样说:
echo"<form name='Biology_lecture11.php' method='post'>";
在你的while循环中。所以它不止一次被推出。尝试在循环之前的行中写入该部分。我看到了几个问题: 1:您的更新代码在每次加载页面时都在运行。将更新块移到if-isset$_POST['submit']{}块中。 2:当你打印学生时,你为每个学生创建一个名为present的输入。如果要填写此字段并提交数据,则只会将最后一个字段添加到数据库中。 3:您没有更新特定的学生。我会将输入字段更改为复选框,并将其命名为present[$rows[student_id]]。 然后,在处理页面后,循环遍历$\u POST['present']的键/值。并更新其中的所有学生
foreach (array_keys($_POST['present']) as $student_id) {
if (is_numeric($student_id)) {
$sql="UPDATE course_attendance SET present='true' WHERE course_id='101' AND week_id='2' and student_id='$student_id'";
}
}
如果考勤表中没有自动填写学生,则必须修改更新。如果每个学生都不在,你必须运行一个查询,看看他们是否存在。如果他们不插入行。如果有,则更新该行。
4:在打开表之前将开始标记移动到student循环外部。需要考虑两件事:第一,表单元素复制。正如上面的评论所说,去掉这条线
echo"<form name='Biology_lecture11.php' method='post'>";
希望有帮助 这应该适合你。这将为每个学生分配一个唯一的现值,然后在回发时检查该值,如果设置了该值,则该值将被清除并用于更新出勤中的学生记录 我还将PHP中的echo'd HTML提取为HTML,并将表单移到表外,这可能会在某些浏览器中导致问题
<?php
// Update present values
if (isset($_POST['submit']))
{
// Get a list of student ids to check
$idsResult = mysql_query("SELECT student_id from students");
while($idRow = mysql_fetch_array($idsResult))
{
// if the textbox for this student is set
if(isset($_POST['present'.$idRow['student_id']]) && !empty($_POST['present'.$idRow['student_id']]))
{
// Clean the user input, then escape and update the database
$cleanedPresent = htmlspecialchars(strip_tags($_POST['present'.$idRow['student_id']]));
$sql = "UPDATE course_attendance SET present='".mysql_real_escape_string($present)."' WHERE course_id='101' AND week_id='2' AND student_id=".$idRow['student_id'];
$result = mysql_query($sql);
if($result){
echo "Successfully logged the attendance for ID ".$idRow['student_id'];
}
else {
echo "ERROR updating on ID ".$idRow['student_id'];
}
}
}
}
$test3= "SELECT * FROM course_attendance, students, courses, attendance WHERE course_attendance.course_id=courses.course_id AND course_attendance.week_id=attendance.week_number_id AND course_attendance.student_id= students.student_id AND courses.course_id='101' AND attendance.week_number_id='2' ";
$result = mysql_query($test3) or die(mysql_error());
?>
<form name='Biology_lecture11.php' method='post'>
</br>
<table border='1' align='center'>
<tr>
<th><strong>Student ID</strong></th>
<th><strong>First Name </strong></th>
<th><strong>Last Name</strong></th>
<th><strong>Present</strong></th>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
echo "<tr><td width='100' align='center'>" .$rows['student_id'].
"</td><td width='120' align='center'>" .$rows['fname'].
"</td><td width='120' align='center'>" .$rows['lname'].
"</td><td><input type='text' name='present".$rows['student_id']."' value=" .$rows['present'] . ">";
}
?>
</table>
<input type='submit' name='Submit' value='Submit'>
</form>
更好的替代方法:如果当前值可以设置为简单的0/1或真/假,那么为每个学生使用复选框会更容易。在回发中,您可以通过选中每个指示在场学生的复选框来检索一个值数组,然后在一个查询中更新数据库表。这也可以防止恶意文本输入
备选代码:
<?php
// Update present values
if (isset($_POST['submit']))
{
// Get a list of student ids to check
$idsResult = mysql_query("SELECT student_id from students");
$presentIds = array();
$absentIds = array();
while($idRow = mysql_fetch_array($idsResult))
{
// If the student's checkbox is checked, add it to the presentIds array.
if(isset($_POST['present'.$idRow['student_id']]))
{
$presentIds[] = $idRow['student_id'];
}
else
{
$absentIds[] = $idRow['student_id'];
}
}
// Convert array to string for query
$idsAsString = implode(",", $presentIds);
// You can set present to whatever you want. I used 1.
$sql = "UPDATE course_attendance SET present='1' WHERE course_id='101' AND week_id='2' AND student_id IN (".$idsAsString.")";
$result = mysql_query($sql);
if($result){
echo "Successfully logged the attendance for IDs ".$idsAsString;
}
else {
echo "ERROR updating on IDs ".$idsAsString;
}
// OPTIONAL: Mark absent students as '0' or whatever other value you want
$absentIdsAsString = implode(",", $absentIds);
// You can set present to whatever you want. I used 1.
$absentQuery = "UPDATE course_attendance SET present='0' WHERE course_id='101' AND week_id='2' AND student_id IN (".$absentIdsAsString.")";
$absentResult = mysql_query($absentQuery);
if($absentResult){
echo "Successfully logged absence for IDs ".$absentIdsAsString;
}
else {
echo "ERROR updating absence on IDs ".$absentIdsAsString;
}
}
$test3= "SELECT * FROM course_attendance, students, courses, attendance WHERE course_attendance.course_id=courses.course_id AND course_attendance.week_id=attendance.week_number_id AND course_attendance.student_id= students.student_id AND courses.course_id='101' AND attendance.week_number_id='2' ";
$result = mysql_query($test3) or die(mysql_error());
?>
<form name='Biology_lecture11.php' method='post'>
</br>
<table border='1' align='center'>
<tr>
<th><strong>Student ID</strong></th>
<th><strong>First Name </strong></th>
<th><strong>Last Name</strong></th>
<th><strong>Present</strong></th>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
echo "<tr><td width='100' align='center'>" .$rows['student_id'].
"</td><td width='120' align='center'>" .$rows['fname'].
"</td><td width='120' align='center'>" .$rows['lname'].
"</td><td><input type='checkbox' name='present".$rows['student_id']."' ";
// NOTE: REPLACE 1 with whatever value you store in the database for being present.
// I used 1 since the update at the top of the code uses 0 and 1.
if($rows['present']=='1')
{
echo "checked='checked' ";
}
// With a checkbox, you don't need to assign it a value.
echo "value=" .$rows['present'];
echo ">";
}
?>
</table>
<input type='submit' name='Submit' value='Submit'>
</form>
你连接到数据库了吗?是的,我连接到数据库了。看起来你正在更新所有学生的现值,因为你的唯一检查是课程id和周id。如果你的while循环包含多条记录,您将有多个元素实例,MySQL中的表是course_Attention:Attention_id,week_id FK,course_idFK,student_idFK,PresentYou真的给您的儿子起名叫Robert吗;升降台课程_出勤率;-?哦,是的,小鲍比桌子,我们叫他这行不通。while循环中仍然存在表单元素的重复。@Sumit感谢您写出它,但是它仍然没有更新每个特定学生的字段,这是一个奇怪的问题,因为代码在逻辑上似乎是这样的correct@JasjootMuhdar检查我编辑的答案,有遗漏不工作,我不知道为什么!这真的很烦人,我很感激你花时间在这个家伙身上@SumitBijvaniDo你认为如果我有复选框的话可能会更容易吗?我现在正在查看,谢谢你花时间来帮助我,我会让你知道它是否有效!这是行不通的。因为您已将表单标记放入表中,并在表标记后关闭它,所以我发现的第二个问题是,提交按钮名为Submit,并且在开始时您要求$U POST['Submit']。它们应该有相同的名字,注意大写字母和小写字母。我要试试这个out@Gareth对不起,这让人很痛苦,你知道如何用复选框而不是字母来编码吗textbox@JasjootMuhdar是的,我很快会把它添加到答案中。非常感谢您,我在几天后就拿到了我的最后一年项目演示,你会救我的命@JasjootMuhdar请查看我的备用代码。您可以使用当前用于将学生标记为数据库中存在的任何内容替换0和1。
<?php
// Update present values
if (isset($_POST['submit']))
{
// Get a list of student ids to check
$idsResult = mysql_query("SELECT student_id from students");
$presentIds = array();
$absentIds = array();
while($idRow = mysql_fetch_array($idsResult))
{
// If the student's checkbox is checked, add it to the presentIds array.
if(isset($_POST['present'.$idRow['student_id']]))
{
$presentIds[] = $idRow['student_id'];
}
else
{
$absentIds[] = $idRow['student_id'];
}
}
// Convert array to string for query
$idsAsString = implode(",", $presentIds);
// You can set present to whatever you want. I used 1.
$sql = "UPDATE course_attendance SET present='1' WHERE course_id='101' AND week_id='2' AND student_id IN (".$idsAsString.")";
$result = mysql_query($sql);
if($result){
echo "Successfully logged the attendance for IDs ".$idsAsString;
}
else {
echo "ERROR updating on IDs ".$idsAsString;
}
// OPTIONAL: Mark absent students as '0' or whatever other value you want
$absentIdsAsString = implode(",", $absentIds);
// You can set present to whatever you want. I used 1.
$absentQuery = "UPDATE course_attendance SET present='0' WHERE course_id='101' AND week_id='2' AND student_id IN (".$absentIdsAsString.")";
$absentResult = mysql_query($absentQuery);
if($absentResult){
echo "Successfully logged absence for IDs ".$absentIdsAsString;
}
else {
echo "ERROR updating absence on IDs ".$absentIdsAsString;
}
}
$test3= "SELECT * FROM course_attendance, students, courses, attendance WHERE course_attendance.course_id=courses.course_id AND course_attendance.week_id=attendance.week_number_id AND course_attendance.student_id= students.student_id AND courses.course_id='101' AND attendance.week_number_id='2' ";
$result = mysql_query($test3) or die(mysql_error());
?>
<form name='Biology_lecture11.php' method='post'>
</br>
<table border='1' align='center'>
<tr>
<th><strong>Student ID</strong></th>
<th><strong>First Name </strong></th>
<th><strong>Last Name</strong></th>
<th><strong>Present</strong></th>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
echo "<tr><td width='100' align='center'>" .$rows['student_id'].
"</td><td width='120' align='center'>" .$rows['fname'].
"</td><td width='120' align='center'>" .$rows['lname'].
"</td><td><input type='checkbox' name='present".$rows['student_id']."' ";
// NOTE: REPLACE 1 with whatever value you store in the database for being present.
// I used 1 since the update at the top of the code uses 0 and 1.
if($rows['present']=='1')
{
echo "checked='checked' ";
}
// With a checkbox, you don't need to assign it a value.
echo "value=" .$rows['present'];
echo ">";
}
?>
</table>
<input type='submit' name='Submit' value='Submit'>
</form>