无法找出SQL字段中数据的重复项和随机单元格删除(PHP/MYSQL)

无法找出SQL字段中数据的重复项和随机单元格删除(PHP/MYSQL),php,mysql,Php,Mysql,我有一个考勤页面,该页面通过以下循环输出班级中的学生列表: $sql10 = "SELECT class.name, student_to_class.class_id, student_to_class.student_id FROM student_to_class INNER JOIN class ON class.id=student_to_class.class_id WHERE class.name = '$classid'"; $result10 = mysql_qu

我有一个考勤页面,该页面通过以下循环输出班级中的学生列表:

  $sql10 = "SELECT class.name, student_to_class.class_id, student_to_class.student_id
FROM 
student_to_class
INNER JOIN 
class
ON class.id=student_to_class.class_id
WHERE
class.name = '$classid'";

  $result10 = mysql_query($sql10) or die(mysql_error());
  while ($row = mysql_fetch_array($result10)) {
      $student = $row['student_id'];
      $classid = $row['class_id'];
      $sql3 = "select * from student where id = '$student'";
      $result3 = mysql_query($sql3) or die(mysql_error());
      $row3 = mysql_fetch_assoc($result3);
      $studentfname = $row3['first_name'];
      $studentlname = $row3['last_name'];

      $sql4 = "select * from student where first_name = '$studentfname' AND last_name = '$studentlname'";

      $result4 = mysql_query($sql4) or die(mysql_error());
      $row4 = mysql_fetch_assoc($result4);
      $studentrfid = $row4['rfid'];

      $sql5 = "select * from class where id = '$classid'";
      $result5 = mysql_query($sql5) or die(mysql_error());
      $row5 = mysql_fetch_assoc($result5);
      $class_name = $row5['name'];

      //Define the default variables assuming attendance hasn't been taken.
      $david = "select * from student where rfid='$studentrfid'";
      $davidresult = mysql_query($david) or die(mysql_error());
      $drow = mysql_fetch_assoc($davidresult);



      if (($drow['excused'] == '1') && ($drow['excuseddate'] == $date)) {
          //if($drow['excuseddate'] == $date;
          $excusedabsense = '<option value="Excused Absense" label="Excused Absense" selected="selected">Excused Absense</option>';
      } else {
          $excusedabsense = '';
      }
      $presentpunctual = '<option value="Present" label="Present">Present</option>';
      $presenttardy = '<option value="Tardy" label="Tardy">Tardy</option>';
      $unexcusedabsense = '<option value="Absent" label="Absent">Absent</option>';


      if (isset($_POST['editdate'])) {
          $date = $_POST['date'];
      }

      $realfname = $studentfname;
      $reallname = $studentlname;



      $sql4 = "select * from attendance_main where StudentID = '$studentrfid' AND date = '$date' AND classID = '$class_name'";

      $result4 = mysql_query($sql4) or die(mysql_error());
      $row4 = mysql_fetch_assoc($result4);


      if ($row4['status'] == "Present") {
          $presentpunctual = '<option value="Present" label="Present" selected="selected">Present</option>';
      } else {
          $presentpunctual = '<option value="Present" label="Present">Present</option>';
      }
      if ($row4['status'] == "Tardy") {
          $presenttardy = '<option value="Tardy" label="Tardy" selected="selected">Tardy</option>';
      } else {
          $presenttardy = '<option value="Tardy" label="Tardy">Tardy</option>';
      }
      if ($row4['status'] == "Absent") {
          $unexcusedabsense = '<option value="Absent" label="Absent" selected="selected">Absent</option>';
      } else {
          $unexcusedabsense = '<option value="Absent" label="Absent">Absent</option>';
      }




      $b++;
      echo "<tr>";
      if (!isset($dateform)) {
          $dateform = date('m/d/Y');
      }
      $date = date('m/d/Y');
      echo '<td><iframe src="flag.php?&flagdate=' . $dateform . '&curdate=' . $date . '&class=' . $classid . '&flag=1&user=' . $studentrfid . '&curflag=' . $realrfid['flag'] . '&flagclass=' . $classname . '" width="50" height="30" frameborder="0" scrolling="no"> </iframe></td>';


      //Yesterday
      $sql8 = "select * from attendance_main where StudentID = '$studentrfid' AND date='$yesterdaysql' AND classID = '$class_name'";
      $result8 = mysql_query($sql8) or die(mysql_error());
      $tooltiprow = mysql_fetch_assoc($result8);
      if (mysql_num_rows($result8) == 0) {
          $tooltipresult_yesterday = "N/A";
      } else {
          $tooltipresult_yesterday = $tooltiprow['status'];
      }

      //2 days
      $sql8 = "select * from attendance_main where StudentID = '$studentrfid' AND date='$days2sql' AND classID = '$classid'";
      $result8 = mysql_query($sql8) or die(mysql_error());
      $tooltiprow = mysql_fetch_assoc($result8);
      if (mysql_num_rows($result8) == 0) {
          $tooltipresult_2days = "N/A";
      } else {
          $tooltipresult_2days = $tooltiprow['status'];
      }

      //3 days
      $sql8 = "select * from attendance_main where StudentID = '$studentrfid' AND date='$days3sql' AND classID = '$class_name'";
      $result8 = mysql_query($sql8) or die(mysql_error());
      $tooltiprow = mysql_fetch_assoc($result8);
      if (mysql_num_rows($result8) == 0) {
          $tooltipresult_3days = "N/A";
      } else {
          $tooltipresult_3days = $tooltiprow['status'];
      }



      $tooltip = "<b>" . $yesterday . ":</b> " . $tooltipresult_yesterday . " - <b>" . $days2 . ":</b> " . $tooltipresult_2days . " - <b>" . $days3 . ":</b> " . $tooltipresult_3days;


      echo "
<!-- Loop #" . $b . " --> <td><a href='#'";
?> onMouseover="ddrivetip('<?php
      echo $tooltip;
?>')"; onMouseout="hideddrivetip()"> <?php
      echo $realfname . " " . $reallname . "</a></td>";
      echo '<td>
<select name="status' . $b . '">
' . $presentpunctual . '
' . $presenttardy . '
' . $excusedabsense . '
' . $unexcusedabsense . '
</select>
' . $hiddenfield . '
<input type="hidden" name="i" value="' . $b . '" />
<input type="hidden" name="studentid' . $b . '" value="' . $studentrfid . '">
<input type="hidden" name="classid" value="' . $class_name . '"></td>
<td><input type="text" name="comments' . $b . '" size="40" /></td></tr>
<!-- End Loop -->';
  }
}
}
if (isset($_GET['update'])) {
      mysql_query("UPDATE teacher_accounts SET attendance = '1' WHERE username = '$username'") or die(mysql_error());
      $error = 0;
      $limit = $_GET['i'];
      $starter = 0;
      $num = 0;


      while ($starter < $limit) {
          $num++;
          $statusinc = "status" . $num;
          $studentinc = "studentid" . $num;
          $commentsinc = "comments" . $num;
          $starter++;
          $studentID = $_GET[$studentinc];
          $status = $_GET[$statusinc];
          $comments = $_GET[$commentsinc];
          $date = date("m/d/Y");




          $sql = "select * from student where id = '$studentID'";
          $result = mysql_query($sql) or die(mysql_error());
          $row = mysql_fetch_assoc($result);



          $classid = $_GET['classid'];


          if (isset($_GET['dateedit'])) {
              $date = $_GET['dateedit'];

              $count = "select * from attendance_main where StudentID = '$studentID' AND date = '$date' AND classID='$classid'";
              $cresult = mysql_query($count) or die(mysql_error());
              if (mysql_num_rows($cresult) > 0) {
                  $sql = "UPDATE attendance_main SET status='$status',comments='$comments',date='$date',classID='$classid' where StudentID = '$studentID'";
              } else {
                  $sql = "INSERT INTO attendance_main (StudentID,status,comments,date,classID) VALUES ('$studentID','$status','$comments','$date','$classid')";
              }

              if (mysql_query($sql)) {
                  $return = "<h3>Successfully updated the attendance.</h3>";
              }
          } else {
              $count = "select * from attendance_main where StudentID = '$studentID' AND date = '$date' AND classID='$classid'";

              $cresult = mysql_query($count) or die(mysql_error());
              if (mysql_num_rows($cresult) > 0) {
                  $sql = "UPDATE attendance_main SET status='$status',comments='$comments',date='$date',classID='$classid' where StudentID = '$studentID'";

                  if (mysql_query($sql)) {
                      $return = "<h3>Successfully updated the attendance for " . $num . " students.</h3>";
                  }
              } else {
                  $sql = "INSERT INTO attendance_main (StudentID,status,comments,date,classID) VALUES ('$studentID','$status','$comments','$date','$classid')";
                  if (mysql_query($sql)) {
                      $return = "<h3>Successfully inserted today's attendance for " . $num . " students.";
                  }
              }
          }
      }
      echo $return;
它基本上打印出学生姓名和一个状态下拉列表,如果当天进行了出勤,则状态将是数据库中设置的任何状态。日期、标志和工具提示功能是额外添加的功能。日期为前几天,工具提示在鼠标悬停时显示前几天的出席情况

此数据通过以下循环执行:

  $sql10 = "SELECT class.name, student_to_class.class_id, student_to_class.student_id
FROM 
student_to_class
INNER JOIN 
class
ON class.id=student_to_class.class_id
WHERE
class.name = '$classid'";

  $result10 = mysql_query($sql10) or die(mysql_error());
  while ($row = mysql_fetch_array($result10)) {
      $student = $row['student_id'];
      $classid = $row['class_id'];
      $sql3 = "select * from student where id = '$student'";
      $result3 = mysql_query($sql3) or die(mysql_error());
      $row3 = mysql_fetch_assoc($result3);
      $studentfname = $row3['first_name'];
      $studentlname = $row3['last_name'];

      $sql4 = "select * from student where first_name = '$studentfname' AND last_name = '$studentlname'";

      $result4 = mysql_query($sql4) or die(mysql_error());
      $row4 = mysql_fetch_assoc($result4);
      $studentrfid = $row4['rfid'];

      $sql5 = "select * from class where id = '$classid'";
      $result5 = mysql_query($sql5) or die(mysql_error());
      $row5 = mysql_fetch_assoc($result5);
      $class_name = $row5['name'];

      //Define the default variables assuming attendance hasn't been taken.
      $david = "select * from student where rfid='$studentrfid'";
      $davidresult = mysql_query($david) or die(mysql_error());
      $drow = mysql_fetch_assoc($davidresult);



      if (($drow['excused'] == '1') && ($drow['excuseddate'] == $date)) {
          //if($drow['excuseddate'] == $date;
          $excusedabsense = '<option value="Excused Absense" label="Excused Absense" selected="selected">Excused Absense</option>';
      } else {
          $excusedabsense = '';
      }
      $presentpunctual = '<option value="Present" label="Present">Present</option>';
      $presenttardy = '<option value="Tardy" label="Tardy">Tardy</option>';
      $unexcusedabsense = '<option value="Absent" label="Absent">Absent</option>';


      if (isset($_POST['editdate'])) {
          $date = $_POST['date'];
      }

      $realfname = $studentfname;
      $reallname = $studentlname;



      $sql4 = "select * from attendance_main where StudentID = '$studentrfid' AND date = '$date' AND classID = '$class_name'";

      $result4 = mysql_query($sql4) or die(mysql_error());
      $row4 = mysql_fetch_assoc($result4);


      if ($row4['status'] == "Present") {
          $presentpunctual = '<option value="Present" label="Present" selected="selected">Present</option>';
      } else {
          $presentpunctual = '<option value="Present" label="Present">Present</option>';
      }
      if ($row4['status'] == "Tardy") {
          $presenttardy = '<option value="Tardy" label="Tardy" selected="selected">Tardy</option>';
      } else {
          $presenttardy = '<option value="Tardy" label="Tardy">Tardy</option>';
      }
      if ($row4['status'] == "Absent") {
          $unexcusedabsense = '<option value="Absent" label="Absent" selected="selected">Absent</option>';
      } else {
          $unexcusedabsense = '<option value="Absent" label="Absent">Absent</option>';
      }




      $b++;
      echo "<tr>";
      if (!isset($dateform)) {
          $dateform = date('m/d/Y');
      }
      $date = date('m/d/Y');
      echo '<td><iframe src="flag.php?&flagdate=' . $dateform . '&curdate=' . $date . '&class=' . $classid . '&flag=1&user=' . $studentrfid . '&curflag=' . $realrfid['flag'] . '&flagclass=' . $classname . '" width="50" height="30" frameborder="0" scrolling="no"> </iframe></td>';


      //Yesterday
      $sql8 = "select * from attendance_main where StudentID = '$studentrfid' AND date='$yesterdaysql' AND classID = '$class_name'";
      $result8 = mysql_query($sql8) or die(mysql_error());
      $tooltiprow = mysql_fetch_assoc($result8);
      if (mysql_num_rows($result8) == 0) {
          $tooltipresult_yesterday = "N/A";
      } else {
          $tooltipresult_yesterday = $tooltiprow['status'];
      }

      //2 days
      $sql8 = "select * from attendance_main where StudentID = '$studentrfid' AND date='$days2sql' AND classID = '$classid'";
      $result8 = mysql_query($sql8) or die(mysql_error());
      $tooltiprow = mysql_fetch_assoc($result8);
      if (mysql_num_rows($result8) == 0) {
          $tooltipresult_2days = "N/A";
      } else {
          $tooltipresult_2days = $tooltiprow['status'];
      }

      //3 days
      $sql8 = "select * from attendance_main where StudentID = '$studentrfid' AND date='$days3sql' AND classID = '$class_name'";
      $result8 = mysql_query($sql8) or die(mysql_error());
      $tooltiprow = mysql_fetch_assoc($result8);
      if (mysql_num_rows($result8) == 0) {
          $tooltipresult_3days = "N/A";
      } else {
          $tooltipresult_3days = $tooltiprow['status'];
      }



      $tooltip = "<b>" . $yesterday . ":</b> " . $tooltipresult_yesterday . " - <b>" . $days2 . ":</b> " . $tooltipresult_2days . " - <b>" . $days3 . ":</b> " . $tooltipresult_3days;


      echo "
<!-- Loop #" . $b . " --> <td><a href='#'";
?> onMouseover="ddrivetip('<?php
      echo $tooltip;
?>')"; onMouseout="hideddrivetip()"> <?php
      echo $realfname . " " . $reallname . "</a></td>";
      echo '<td>
<select name="status' . $b . '">
' . $presentpunctual . '
' . $presenttardy . '
' . $excusedabsense . '
' . $unexcusedabsense . '
</select>
' . $hiddenfield . '
<input type="hidden" name="i" value="' . $b . '" />
<input type="hidden" name="studentid' . $b . '" value="' . $studentrfid . '">
<input type="hidden" name="classid" value="' . $class_name . '"></td>
<td><input type="text" name="comments' . $b . '" size="40" /></td></tr>
<!-- End Loop -->';
  }
}
}
if (isset($_GET['update'])) {
      mysql_query("UPDATE teacher_accounts SET attendance = '1' WHERE username = '$username'") or die(mysql_error());
      $error = 0;
      $limit = $_GET['i'];
      $starter = 0;
      $num = 0;


      while ($starter < $limit) {
          $num++;
          $statusinc = "status" . $num;
          $studentinc = "studentid" . $num;
          $commentsinc = "comments" . $num;
          $starter++;
          $studentID = $_GET[$studentinc];
          $status = $_GET[$statusinc];
          $comments = $_GET[$commentsinc];
          $date = date("m/d/Y");




          $sql = "select * from student where id = '$studentID'";
          $result = mysql_query($sql) or die(mysql_error());
          $row = mysql_fetch_assoc($result);



          $classid = $_GET['classid'];


          if (isset($_GET['dateedit'])) {
              $date = $_GET['dateedit'];

              $count = "select * from attendance_main where StudentID = '$studentID' AND date = '$date' AND classID='$classid'";
              $cresult = mysql_query($count) or die(mysql_error());
              if (mysql_num_rows($cresult) > 0) {
                  $sql = "UPDATE attendance_main SET status='$status',comments='$comments',date='$date',classID='$classid' where StudentID = '$studentID'";
              } else {
                  $sql = "INSERT INTO attendance_main (StudentID,status,comments,date,classID) VALUES ('$studentID','$status','$comments','$date','$classid')";
              }

              if (mysql_query($sql)) {
                  $return = "<h3>Successfully updated the attendance.</h3>";
              }
          } else {
              $count = "select * from attendance_main where StudentID = '$studentID' AND date = '$date' AND classID='$classid'";

              $cresult = mysql_query($count) or die(mysql_error());
              if (mysql_num_rows($cresult) > 0) {
                  $sql = "UPDATE attendance_main SET status='$status',comments='$comments',date='$date',classID='$classid' where StudentID = '$studentID'";

                  if (mysql_query($sql)) {
                      $return = "<h3>Successfully updated the attendance for " . $num . " students.</h3>";
                  }
              } else {
                  $sql = "INSERT INTO attendance_main (StudentID,status,comments,date,classID) VALUES ('$studentID','$status','$comments','$date','$classid')";
                  if (mysql_query($sql)) {
                      $return = "<h3>Successfully inserted today's attendance for " . $num . " students.";
                  }
              }
          }
      }
      echo $return;
由于某些原因,数据有时插入不正确。例如,教师可能会在2011年8月2日提交特定班级的出勤情况,某些学生可能会在该出勤情况下出现两次。根据代码,情况不应该是这样,因为它应该首先检查它们是否存在,如果存在,则更新记录,而不是插入

我也看到过记录被随机删除的情况。教师出勤时,所有状态都自动设置为“出席”。然而,当我在数据库中搜索某个日期的记录时,有两个学生丢失了记录,除非将其删除,否则这是不可能的

有人知道为什么会这样吗?我尝试过自己复制它,反复提交表单,在处理后刷新页面,等等,但都没有效果


谢谢你的帮助

检查记录是否存在的查询正在查找所有3条记录。1$studentID、2$classid和3$classid,但是UPDATE语句只查找$studentID

我建议您在StudentID、date、classID上创建一个主键或唯一索引,然后使用MySql


不要忘记使用mysql\u real\u escape\u字符串清理数据库输入,例如$status=mysql\u real\u escape\u string$\u GET[$statusinc]

你可以用一个唯一的密钥在db级别停止重复。我不想完全停止重复。比如,我希望有重复的班级和日期,因为一个班级里有不止一个学生,但我不希望在某个班级的某个特定日期有重复的学生。你怎么把它放在钥匙里?如果发生这种情况,老师提交考勤时会出现错误吗?你可以指定一个多列唯一键,在特定的日期为特定的班级阻止重复的学生,如果你不使用该错误,它仍然是一个很好的数据完整性检查哦,天哪。我甚至不敢相信我怎么没有注意到更新查询只查看一个条件这一事实。有另一双眼睛会有帮助。非常感谢你的建议!