2017-03-28 4 views
0

codeigniter가있는 학교 출근 추적기를 만들려고합니다. Student_no와 라디오 버튼 값 Present 또는 Absent를 데이터베이스에 입력하는 방법을 모르겠습니다. student_no 및 라디오 버튼의 값은 모두 내 컨트롤러에 NULL로 게시되고 있습니까?PHP codeigniter를 사용하여 데이터베이스에 행 값을 여러 개 추가하는 방법

내 컨트롤러 파일

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

내보기 양식 필드가 student_no입니다

 <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> 
+2

이 코드가 HTML에서 'name = "attendance "'를 생성하는 브라우저에서'page source'를보십시오. 그러면 적어도 하나의 실수가 있음을 알아야합니다 – RiggsFolly

+0

"name"속성 안에 공백을 넣지 마십시오. "현재"라디오 버튼의 "이름"속성에는 "출석"과 학생 사이에 공백이 포함되어 있습니다. – alpadev

+0

@RiggsFolly 감사합니다. 그래서 '출석'의 마지막에 학생 번호를 붙이고 있습니다 :/그것을 제거하면 두 행 사이에서 4 개의 라디오 버튼 중 하나만 선택할 수 있습니다. newb에 대한 제안 사항이 있습니까? – Glen

답변

0

? 나는 그것을 보지 않는다, 그것은 미정의 통지를 던져야한다. $ 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); 

필요하지 않습니다.

편집 :
입력 필드 코드를 업데이트했습니다. 각 학생이 자신의 양식을 갖도록 양식 태그를 foreach 문 안에 넣습니다.

각 양식에는 player_id를 전달하는 숨겨진 필드가 필요합니다.
당신은 또한 외부 <form> 태그를 넣을 수 있습니다 : 컨트롤러에서

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

, POST 데이터의 유효성을 검사하고 통과 player_id 사용

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

또는 유사한

EDIT (2) foreach 문을 사용하지만 컨트롤러의 모든 POST 데이터를 반복해야합니다. 이렇게하면 전체 양식에 하나의 제출 버튼을 가질 수는 있지만 백엔드 작업은 더욱 복잡해집니다.

+0

답장을 보내 주셔서 감사합니다! 이게 내가 정확히 player_id 값을 얻는 방법을 정확히 이해하지 못하는 곳이라고 생각합니다. 나는 완전히 php와 codeigniter에 익숙하다 ... $ this-> db-> set ($ data); 각 행마다 player_id 값을 얻는 방법에 대해 알고 있습니까? – Glen

+0

업데이트 됨 – qwertzman