2017-05-06 112 views
0

問題遍歷所有輸入值

所以我的問題是,我有去到數據庫並獲取學生數據的一些PHP代碼。然後該程序將學生信息輸出到屏幕上。信息放在input內,因爲用戶將能夠編輯信息。當用戶編輯信息並點擊Update時,我需要獲取有關數據庫更新的信息。我的問題是,我不知道如何循環所有輸入。

PHP代碼 - 當用戶點擊Update

foreach ($_POST as $data) { 
    echo $data . "<br>"; 
} 

PHP代碼 - 允許用戶編輯

<form action="server/edit/students.php" method="post"> 

        <table> 
         <tr> 
          <th>Student ID</th> 
          <th>Firstname</th> 
          <th>Lastname</th> 
          <th>Teacher's Firstname</th> 
          <th>Teacher's Lastname</th> 
         </tr> 

         <?php 

          // get student info 
          $getStudent = $link->prepare("SELECT * FROM students"); 
          $getStudent->execute(); 
          $getStudent = $getStudent->fetchAll(PDO::FETCH_ASSOC); 

          foreach ($getStudent as $student) { 
           $studentID = $student['studentID']; 
           $firstname = $student['firstname']; 
           $lastname = $student['lastname']; 
           $teacherID = $student['teacherID']; 
           $getTeacher = $link->prepare("SELECT * FROM teachers 
                  WHERE teacherID = :teacherID"); 

           $getTeacher->execute(array(
            "teacherID" => $teacherID 
           )); 

           $getTeacher = $getTeacher->fetchAll(); 

           foreach ($getTeacher as $teacher) { 
            $teacherFirstname = $teacher['firstname']; 
            $teacherLastname = $teacher['lastname']; 

            echo " 
             <tr> 
              <td><input type='text' value='$studentID'></td> 
              <td><input type='text' value='$firstname'></td> 
              <td><input type='text' value='$lastname'></td> 
              <td><input type='text' value='$teacherFirstname'></td> 
              <td><input type='text' value='$teacherLastname'></td> 
             </tr> 
            "; 
           } 

          } 

         ?> 
        </table> 

        <button type="submit" name="update">Update</button> 

       </form> 
+0

首先,我沒有看到任何形式的標籤。我想你只是忽略了那個?另一件事,你需要添加'name'-屬性到你的輸入字段,否則他們不會被髮布。 –

+0

我編輯了我的問題 –

+1

我還建議您在第一次查詢時使用'JOIN'來獲取教師,而不是在每次迭代中再次調用數據庫。 –

回答

0

所以,我所做的是我用一個變量來跟蹤的數據行。

代碼

<?php 

          // get student info 
          $getStudent = $link->prepare("SELECT * FROM students ORDER BY studentID"); 
          $getStudent->execute(); 
          $getStudent = $getStudent->fetchAll(PDO::FETCH_ASSOC); 

          $value = 0; // counts rows 

          // loop through each student 
          foreach ($getStudent as $student) { 
           $studentID = $student['studentID']; 
           $firstname = $student['firstname']; 
           $lastname = $student['lastname']; 
           $teacherID = $student['teacherID']; 

           // get teacher 
           $getTeacher = $link->prepare("SELECT * FROM teachers 
                  WHERE teacherID = :teacherID"); 

           $getTeacher->execute(array(
            "teacherID" => $teacherID 
           )); 

           $getTeacher = $getTeacher->fetchAll(); 

           // loop through each teacher 
           foreach ($getTeacher as $teacher) { 
            $teacherFirstname = $teacher['firstname']; 
            $teacherLastname = $teacher['lastname']; 

            // output data 
            echo " 
             <tr> 
              <td><input type='text' name='studentID-$value' value='$studentID'></td> 
              <td><input type='text' name='firstname-$value' value='$firstname'></td> 
              <td><input type='text' name='lastname-$value' value='$lastname'></td> 
              <td><input type='text' name='teacherFirst-$value' value='$teacherFirstname'></td> 
              <td><input type='text' name='teacherLast-$value' value='$teacherLastname'></td> 
             </tr> 
            "; 
           } 

           // add to row 
           $value += 1; 

          } 

         ?>