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

これはあなたのために働くはずです。これにより、各生徒に一意の値が割り当てられます。このpresent値は、ポストバックでチェックされ、設定されている場合は、クリーンアップされて、出席している生徒の記録を更新するために使用されます。

また、PHPでエコーされた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またはtrue / falseに設定できる場合は、各生徒のチェックボックスを使用する方が簡単です。ポストバックでは、出席している学生を示す各チェックボックスをオンにして値の配列を取得し、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 に答える
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

考慮すべき 2 つの点: まず、フォーム要素の複製があります。上記のコメントが言ったように、行を取り出します

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

ループから。

次に、ステートメントはすべての学生を更新UPDATEします。SQLステートメントにトークンが必要です。このようなもの:WHERE

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

table タグ内と 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

私が見る間違いの1つは、あなたがこれを置くことです:

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

あなたのwhileループで。なので何度も出されます。ループの前の行にその部分を書いてみてください。

于 2013-03-12T20:10:03.220 に答える