2017-05-07 74 views
0

問題任何方式將數據拆分成數組?

因此,一個有一些PHP代碼,從數據庫,並輸出該信息獲取學生信息。該信息是在input,使用戶輸出可以編輯信息。然後,當用戶完成編輯信息時,我需要獲取我擁有的數據。我的問題是,我不知道如何打破這些信息,因此很容易讓我改變在數據庫中的信息。

學生表

studentID | firstname | lastname | teacherID 

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

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

          } 

         ?> 
        </table> 

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

       </form> 

PHP代碼,改變了DB代碼

$dataCount = 0; 

foreach ($_POST as $data) { 
    if($dataCount % 5 === 0) { 
     echo "<br><br>"; 
     echo $data . ", "; 
    } else { 
     echo $data . ", "; 
    } 

    $dataCount++; 

} 

什麼形式循環,如屏幕

StudentID  Firstname  Lastname  Teacher's Firstname  Teacher's Lastname 
_______________________________________________________________________________________ 
1    Bill   Roy   Jonathan     Kung 
2    Travis   Roy   Shanin     Harnik 
+0

你的問題是混亂的。 $ _POST變量不是包含數據的數組嗎? – styl3r

+0

但我不知道如何打破這些信息,因此很容易讓我改變在數據庫中的信息。 –

+0

' 「UPDATE學生SET姓= '{$ DB-> real_escape_query($ _ POST [' FIRSTNAME '])' WHERE StudentID = '{$ _ POST [' StudentID ']}'」' – styl3r

回答

1

做的最簡單的事情上是在你的輸入字段使用數組名。這樣你$_POST將是一堆分組排列的,很容易通過循環:

<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); 
     $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 
       ?> 
     <tr> 
      <td><input type='text' name='student[<?php echo $studentID ?>][firstname]' value='<?php echo $firstname ?>' /></td> 
      <td><input type='text' name='student[<?php echo $studentID ?>][lastname]' value='<?php echo $lastname ?>' /></td> 
      <td><input type='text' name='student[<?php echo $studentID ?>][teacherFirst]' value='<?php echo $teacherFirstname ?>' /></td> 
      <td><input type='text' name='student[<?php echo $studentID ?>][teacherLast]' value='<?php echo $teacherLastname ?>' /></td> 
     </tr> 
       <?php 
      } 
      // add to row 
      $value += 1; 
     } 
     ?> 
    </table> 
    <button type="submit" name="update">Update</button> 
</form> 

當通過$_POST['student']陣列處理的$_POST你環路和密鑰將識別碼與數據的其餘部分被包含在數組中。