簡體   English   中英

如何使用PHP Codeigniter將多個行值添加到數據庫中

[英]How to add multiple row values into database with php codeigniter

我正在嘗試使用Codeigniter制作學校出勤跟蹤器。 我不知道如何獲取Student_no和單選按鈕值Present或Absent來輸入數據庫。 student_no和單選按鈕的值都被發布為NULL到我的控制器?

我的控制器文件

function insertAttendance(){

    $student_no=$this->input->post('student_no');
    $attendance=$this->input->post('attendance');

    $data = array(
        'student_no'=>$student_no,
        'attendance'=>$attendance
        );
    //$this->form_validation->set_data($data);

    $this->db->set($data);
    $this->db->insert('attendance',$data);
}

我的觀點

     <h3>ATTENDANCE TRACKER</h3>
                <table class="table table-lg" name="student_no"> 

                  <tr>
                  <th>Student_NO</th>
                  <th>Student name</th>
                  <th>Student DOB</th>
                  <th>Attendance</th>
                  </tr>

                <?php foreach ($query->result_array() as $row): {?>
                    <tr>
                        <td><?php echo $row['student_no'];?></td>

                        <td><?php echo $row['student_name'];?></td>

                        <td><?php echo $row['student_dob'];?></td>
                <tr>
                    <label>

<label><input type="radio" name="attendance[<?php echo $row['player_id']; ?>]"  value="Yes">Present</label> 
    &emsp;
<label><input type="radio" name="attendance[<?php echo  $row['player_id']; ?>]" value="No">Absent</label>

                    </td> </tr>

                    <?php } ?>

                    <?php endforeach; ?>

                    </tr>
                    </tbody>
                    </table>

student_no的表單字段在哪里? 我看不到,它應該引發未定義的通知。 假設$ row ['player_id']不為空,則$ this-> input-> post('attendance')將不存在。

<input type="radio" name="attendance_<?php echo $row['player_id']; ?>" value="Yes">

也許像這樣嘗試並為student_no創建一個表單字段(隱藏字段?)。

你不需要

$this->db->set($data);

因為您已經在構建器的插入部分傳遞了$ data數組。

編輯:
我更新了輸入域代碼。 將表單標簽放在foreach語句中,以便每個學生都有自己的表單。

每個表單都需要一個隱藏字段來傳遞player_id。

<input type="hidden" name="player_id" value="<?php echo $row['player_id']; ?>">

在控制器中,驗證POST數據並使用您通過的player_id:

$this->input->post('attendance_'. $this->input->post('player_id'))

或類似

編輯2:
您還可以將<form>標記放在foreach語句之外,但隨后我們需要遍歷控制器中的所有POST數據。 這使您可以在整個表單上使用一個“提交”按鈕,但確實會使您的后端工作更加復雜。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM