1

I have a checkbox in each table row, and I would like to press the "Save" which will loop over each checkbox: (if checked)-> enter to database that this student has attended else ->enter to database that this student has not attended.

enter image description here

My PHP code:

<table border="1" cellpadding="1" width="550px" style="text-align:center;">
    <th> Student ID </th>
    <th> First Name </th>
    <th> Last Name </th>
    <th> Class ID </th>
    <th> Attended </th>
<?php
    $classid = $_GET['classid'];

    $mysql_host='localhost';
    $mysql_user='root';
    $mysql_password='';
    $con = @mysqli_connect($mysql_host,$mysql_user,$mysql_password);

    if(!$con){
        die('Failed to connect to the database');//if not successful
    }else{
        //echo "Successfully connected to MySQL!";//if successful
        if(@mysqli_select_db($con, 'application_database')){//selecting the database
            //echo '<br>'."Connected to the specified database!";
        }else{
            die('<br>'."Could not connect to the specified database!");
        }
    }

    $sql="SELECT * FROM `student` WHERE class = '$classid'";
    $records=mysqli_query($con,$sql);

    while($student=mysqli_fetch_assoc($records)){

        echo "<tr>";
        echo "<td>".$student['id']."</td>";
        echo "<td>".$student['first_name']."</td>";
        echo "<td>".$student['last_name']."</td>";
        echo "<td>".$student['class']."</td>";
        echo "<td>".'<input type="checkbox" name="attendedCB">'."</td>";
    }

    echo '<form>';
    echo 'Teacher ID: <input type="number" name=teacher_id>';
    echo '<button name="save" type="submit">Save Attendance</button>';
    echo '</form>';


?>
</table>

I have a "student_attendance" table in the db, so I want to add each student along with his/her "attended" checkbox status. Cheers :D

6
  • 1
    Make the checkbox an array <input type="checkbox" name="attendedCB[]"> Then you can loop over $_POST['attendedCB'] Commented Jun 4, 2017 at 16:33
  • 1
    RULE: All inputs have to exist INSIDE the <form>...</form> Commented Jun 4, 2017 at 16:35
  • Using the @ error silencer just keeps you in that dark about your errors. While developing YOU NEED TO KNOW WHAT YOU ARE DOING WRONG Commented Jun 4, 2017 at 16:36
  • 1
    Yeah man, I am sorry that my ignorance triggered you :P, I am self-taught (still learning), so you gotta bear with me :) Commented Jun 4, 2017 at 16:41
  • 1
    But thanks a lot for these helpful advice Commented Jun 4, 2017 at 16:41

2 Answers 2

1
// loop per student
foreach ($_POST ['student'] as $student ) {
    extract($student) // extract array (lazy)
    // if they attended 
    if ( !empty ($attendedCB )) {
        // add them
        $query = "INSERT INTO your_table id,first,last,class,attendedCB VALUES($id, $first, $last, $attendedCB)";
        // then run that query or aggregate them all into one big one and run it
    }
}

Also, change the checkbox name to student['attendedCB'] and this should work.

Sign up to request clarification or add additional context in comments.

4 Comments

Hey man, I tried it, but I sure I messed it up.foreach ($_POST ['student'] as $student ) { extract($student); // extract array (lazy) // if they attended if (!empty($attendedCB)){ // add them // $query = "INSERT INTO your_table id,first,last,class,attendedCB VALUES($id, $first, $last, $attendedCB)"; echo "TEST".$student['id']; }else{ echo "TEST22222".$student['id']; } }
it is giving me these errors (Notice: Undefined index: student in C:\xampp\htdocs\ApplicationDemo\fetch_students_add.php on line 136 Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\ApplicationDemo\fetch_students_add.php on line 136)
Are you posting to the page?
Yeah, I mean just to make sure it works. When the person clicks a button, I want to save the entries to the db without leaving the page.
0

Use id as value of check box by clicking that get the id value. Based on the id value you can do operations(update, delete, edit)

echo "<td><input type='checkbox' name='attendedCB' value ='.$student["id"].'></td>";

1 Comment

There is a bit more to OP's issues... See my comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.