2013-03-12 84 views
1

好,所以我創建一個考勤制度,我想,以紀念在場或不在場的學生,這是我的代碼在MySQL沒有更新,PHP

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

    } 
    ?> 

的問題是,它不更新目前字段在數據庫中,任何人知道什麼是錯的

+0

你連接到數據庫? – Vucko 2013-03-12 20:01:17

+0

是的,我連接到數據庫 – 2013-03-12 20:03:02

+0

它看起來像你正在更新所有學生的現值,因爲你唯一的WHERE檢查是'course_id'和'week_id'。如果while循環包含多條記錄,那麼您將擁有元素' 2013-03-12 20:03:53

回答

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> 
+0

謝謝Gareth我要去試試這個 – 2013-03-12 20:54:27

+0

@Gareth對不起,要成爲一個痛苦,你知道如何用複選框而不是textbox – 2013-03-13 15:18:13

+0

@JasjootMuhdar是的,我會盡快將它添加到答案中。 – 2013-03-13 15:23:53

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> 
+0

這_won't_工作。 while循環中仍然存在重複的表單元素。 – 2013-03-12 20:12:31

+0

@Garreh yah你是對的 – 2013-03-12 20:14:57

+0

@Sumit感謝您寫出來,但是它仍然不會更新每個特定學生的字段,這是一個奇怪的,因爲代碼在邏輯上看起來是正確的 – 2013-03-12 20:17:17

0

一個錯誤,我看到的是,你把這個:

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

在while循環。所以它被推出不止一次。嘗試在循環之前在行中寫入該部分。

+0

試過仍然沒有運氣 – 2013-03-12 20:40:43

+0

第二個問題,我發現是,提交-Button被命名爲「提交」,並在開始時詢問$ _POST ['submit']。它們應該有相同的名稱(注意大寫和小寫) – Jokus 2013-03-12 20:52:31

+0

是的。感謝您指出這一點 – 2013-03-12 21:13:30

0

一對夫婦的問題,我看到:

1:你更新代碼運行的每個頁面加載時間。將更新塊移動到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:將開始標記移動到表格打開和學生循環的外部之前。

0

需要考慮的兩件事:首先,你有表單元素的應用。正如上面的評論所述,拿出這條線

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

    } 
    ?> 

希望它有幫助!

+0

我現在檢查出來,謝謝你花時間幫忙,會讓你知道它是否有效! – 2013-03-12 20:26:04

+0

它不起作用。因爲你已經把'form'標籤放在'table'裏面,並且在表格標籤後關閉它 – 2013-03-12 20:50:39