1

好的,所以我正在创建一个考勤系统,我想标记一个学生是否在场,这是我的代码

  <?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";

  }
  ?>

问题是,它不会更新数据库中的当前字段,任何人都知道出了什么问题

4

5 回答 5

0

您在表格标签和 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>
于 2013-03-12T20:09:16.563 回答
0

我看到的一个错误是,你把这个:

echo"<form name='Biology_lecture11.php' method='post'>";

在你的while循环中。所以它被推出了不止一次。尝试在循环之前的行中写下该部分。

于 2013-03-12T20:10:03.220 回答
0

我看到的几个问题:

1:每次加载页面时,您的 UPDATE 代码都在运行。将更新块移动到 if (isset($_POST['submit'])) {} 块中。
2:当您打印出学生时,您会为每个学生创建一个名为“present”的输入。如果您要填写并提交数据,则只会将最后一个字段添加到数据库中。
3:您没有更新特定的学生。我会将输入字段更改为复选框并将其命名为“present[$rows[student_id]]”。
然后,一旦页面被处理,循环遍历 $_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'";
    }
}

如果出勤表没有自动填写学生,则必须修改 UPDATE。如果每个学生都不在那里,您必须运行查询以查看他们是否存在。如果他们不插入行。如果有,请更新该行。
4:将开始标签移动到表格开始之前和学生循环的外部。

于 2013-03-12T20:18:08.777 回答
0

需要考虑两件事:首先,您有表单元素重复。正如上面的评论所说,取出这条线

echo"<form name='Biology_lecture11.php' method='post'>";

从循环。

其次,该UPDATE语句更新所有学生,您需要WHERE在 SQL 语句中添加一个标记。像这样的东西:

 <?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> ";
    echo"<form name='Biology_lecture11.php' method='post'>";    
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'] . ">";

}
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' AND student_id =  the_student_id";
  $result=mysql_query($sql);

  if($result){
      echo "Successfully logged the attendance";
  }
  else {
      echo"ERROR";

  }
  ?>

希望能帮助到你!

于 2013-03-12T20:19:32.643 回答
0

这应该适合你。这将为每个学生分配一个唯一的present值,然后在回发时对其进行检查,如果设置,它会被清理并用于更新学生的出勤记录。

我还将 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>
于 2013-03-12T20:28:07.933 回答