2016-07-05 3 views
0

각 출석에 대한 체크 박스가 포함 된 학생 출석 테이블을 만들려고합니다. 즉, 데이터베이스에 저장됩니다.다중 체크 박스 만들기 PHP

이것은 내가 지금까지 가지고있는 테이블이며, ajax 함수 호출에서 실행됩니다. 대부분의 데이터는 학생 이름이 포함 된 데이터베이스에서 가져온 것입니다. 이 예 checkAttend.php에서 $numStudent = 5;

echo '<form method="post" action="checkAttend.php"> 
    <table border="1">'; 
$count = 1; 

while(($count < $numStudent) && ($students = mysqli_fetch_assoc($result))) { 
    echo '<tr> 
     <th>' . $count. '</th> 
     <th>' . $students['student_name'] . '</th> 
     <th> <input type="checkbox" name="students[]" /> </th> 
    </tr>'; 
    $count++; 
} 

echo '<th colspan = "3"> 
    <input type="submit" name="button" id="button" value="Submit" /> </th>'; 
echo '</table>'; 
echo '</form>'; 

양식 인쇄 방식 그대로 I가 필요하지만 체크 박스 배열의 값을 출력 할 때 (값이 무엇인지 확인을 위해서

배열의 각 값을 출력합니다) 데이터베이스에 저장하기 전에 포함 : 학생이 결석인지 아닌지

$student_attend[0]  A 
$student_attend[1]  r 
$student_attend[2]  r 
$student_attend[3]  a 
$student_attend[4]  y 

일반적으로 내가 나타 내기 위해 데이터베이스의 해당 필드 값의 어떤 종류를 저장할.

체크 박스 루프를 올바르게 수행하고 있는지 또는 더 많은 것들을 추가해야하는지 알 수 없습니다.

답변

0

, 당신은 다음과 같이 사용할 수 있습니다

if(isset($_POST['button'])){ 
    if(count($_POST['student_id'])>0){ 
    foreach($_POST['student_id'] as $student_id){ 
    mysql_query("update table_name set column_name = '1' where student_id = '".$student_id.'"); 
    // Just use the student id as checkbox value and update it as you just need the check or uncheck the checkbox. 
    } 
    } 
} 
1

다음과 같은 $student_attend 배열을 통해 루프 할 것입니다 : foreachhere

foreach ($student_attend as $value) { 
    // do something with $value 
} 

더 많은 정보를. 당신이

<form method="post" action="checkAttend.php"> 
    <table border="1"> 
    <?php 
    $count=1; 
    while($students = mysqli_fetch_assoc($result)) { 
    ?> 
    <tr> 
     <th><?php echo $count;?></th> 
     <th><?php echo $student['student_name']; ?></th> 
     <th><input type="checkbox" name="students[]" value=<?php echo $student['student_id']; ?>/></th> 
    </tr> 
    <?php 
    } 
    ?> 
</table> 
</form> 

를 사용할 수있는 양식 태그 내부 및 checkAttend.php 페이지에 다음 코드를 작성 : 당신이 당신의 체크 박스에 동적 데이터를 추가하려면