2013-03-12 80 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