dongqiang5865 2013-03-12 19:58
浏览 14
已采纳

没有在mysql,php中更新

Ok, So I am creating a attendance system and I want to mark a student present or absent, this is my code

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

  }
  ?>

The problem is , that it does not update the present field in the database, anyone know whats wrong

  • 写回答

5条回答 默认 最新

  • douzhi4830 2013-03-12 20:28
    关注

    This should work for you. This will assign each student a unique present value, which is then checked on postback and if set, it is cleaned and used to update the student record in attendance.

    I also extracted echo'd HTML in the PHP to HTML, and moved your form outside of your table (it can cause issues in some browsers).

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

    Alternative (better) method: If the present value can be set to a simple 0/1 or true/false, then it would be easier to use a checkbox for each student. In postback, you can then retrieve an array of values from checking each checkbox indicating students who are present, and then update the database table in one query. That also prevents from malicious text input.

    Alternative code:

    <?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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 Keil编译时报错"no source": Error: #5: cannot open source
  • ¥50 操作系统时间无法更新
  • ¥20 Windows11, usb转hdmi,外接显示器无反应
  • ¥20 怎么在JavaFx的TableView中动态地添加数据。
  • ¥15 MFC里的工具栏按钮图标使用外部图片
  • ¥15 如何在 llama.cpp 服务器中实现用户登录功能的优化方案?(语言-c++)
  • ¥15 有会用octave 的吗,急!代做!好偿!
  • ¥15 有一套同城小程序源码,Uniapp前端,php+html+mysql后端 ,能不能教我搭建起来可以运行,我不知道怎样操作
  • ¥15 mac调用java.io接口无法在根目录生成文件
  • ¥15 java微服务节点假死,网关路由时长延迟