2016-05-14 122 views
0

我使用這種形式展現學生名單和單選按鈕/下拉的地方現在不存在的事情,我想知道如何與考勤一起存儲學生的整個數據到我的數據庫如何從窗體考勤數據到mysql數據庫?

<form name="insert-attendance.php" action="insert-attendance.php" method="post"> 
    <?php 
     include('connection.php'); 
     $result = $db->prepare("SELECT * FROM sc_students WHERE Student_Class='$classname' AND Student_Section='$section'"); 
     $result->execute(); 
     for ($i = 0; $row = $result->fetch(); $i++) { 
      ?> 
      <tr> 
       <td><input type="text" name="stuid" value="<?php echo $row['Student_id'] ?>"></input></td> 
       <td><input type="text" name="stuname" value="<?php echo $row['Student_name'] ?>"></input></td> 
       <td><input type="text" name="stuclass" value="<?php echo $row['Student_Class'] ?>"></input></td> 
       <td><input type="text" name="section" value="<?php echo $row['Student_Section'] ?>"></input> 
        <input type="hidden" value="<?php echo date("Y-m-d"); ?>" name="attdate"> 
       <td> 
        <select name="attndc"> 
         <option value="present">PRESENT</option> 
         <option value="absent">ABSENT</option> 
         <option value="leave">LEAVE</option> 
        </select> 
       </td> 
      </tr> 
      <?php 
     } 
    ?> 
    <input type="submit" value="submit"> 
</form> 

,這是我insert.php文件,但它不工作

<?php 
    $studentid = $_POST['stuid']; 
    $stuname = $_POST['stuname']; 
    $stuclass = $_POST['stuclass']; 
    $section = $_POST['section']; 
    $attdate = $_POST['attdate']; 
    $attndc = $_POST['attndc']; 
    include "connection.php"; 
    $values = "$studentid, $stuname, $stuclass, $section, $attdate, $attndc"; 
    $sql = array(); 
    $sql[] = '("' . int($row['student_id']) . '' . mysql_real_escape_string($row['stuclass']) . '", ' . $row['section'] . ', ' . $row['attndc'] . ', ' . $row['attdate'] . ')'; 
    $sql = 'INSERT INTO sc_attendance (student_id, class, section, status,attendance_date) VALUES ' . implode(',', $sql); 
    $stmt = $db->prepare($sql); 
    $db->execute($sql); 
?> 

回答

0

你必須使用名稱陣列像stuid[],stuname[]輸入字段在HTML表單,現在將有相同的名字面向全體學生輸入字段stuid,stuname。在HTML中,如果2個輸入字段具有相同的名稱,則最後一個字段將被視爲最終輸入字段,所有其他輸入字段將被丟棄(事實上,它們的值將被最後一個字段替換)。爲了避免這個替換最後一個字段,你必須使用[]作爲名字,並在數據進入PHP時將它們當作數組處理。在這裏你的形式看起來像:

<form name="insert-attendance.php" action="insert-attendance.php" method="post"> 
    <?php 
     include('connection.php'); 
     $result = $db->prepare("SELECT * FROM sc_students WHERE Student_Class='$classname' AND Student_Section='$section'"); 
     $result->execute(); 
     for ($i = 0; $row = $result->fetch(); $i++) { 
      ?> 
      <tr> 
       <td><input type="text" name="stuid[]" value="<?php echo $row['Student_id'] ?>"></input></td> 
       <td><input type="text" name="stuname[]" value="<?php echo $row['Student_name'] ?>"></input></td> 
       <td><input type="text" name="stuclass[]" value="<?php echo $row['Student_Class'] ?>"></input></td> 
       <td> 
        <input type="text" name="section[]" value="<?php echo $row['Student_Section'] ?>"></input> 
        <input type="hidden" value="<?php echo date("Y-m-d"); ?>" name="attdate[]"> 
       <td> 
        <select name="attndc[]"> 
         <option value="present">PRESENT</option> 
         <option value="absent">ABSENT</option> 
         <option value="leave">LEAVE</option> 
        </select> 
       </td> 
      </tr> 
      <?php 
     } 
    ?> 
    <input type="submit" value="submit"> 
</form> 

現在insert.php你要像對待輸入字段爲數組,這樣你可以得到所有學生的數據,並將其插入到數據庫表:

<?php 
    include "connection.php"; 
    $total = count($_POST['stuid']); 

    for($i=0;$i<$total;$i++){ 
     $studentid = $_POST['stuid'][$i]; 
     $stuname = $_POST['stuname'][$i]; 
     $stuclass = $_POST['stuclass'][$i]; 
     $section = $_POST['section'][$i]; 
     $attdate = $_POST['attdate'][$i]; 
     $attndc = $_POST['attndc'][$i]; 

     $sql = $db->prepare("INSERT INTO sc_attendance (student_id, class, section, status,attendance_date) VALUES (?, ?, ?, ?, ?)"); 
     $sql->bind_param($studentid, $stuclass, $section, $attdate, $attndc); 
     $db->execute($sql); 
    } 
?> 

你有在你的sql語句和查詢中犯了很多錯誤。我已經糾正了你。