2

Currently, I am working on the attendance form. In this interface, it consists rows of student(get from student table). The list of students are viewed in the table. the first column is NO, second column in Birth No, third STudent Name, forth Attendance. in column attendance, there are three radio button which is PT, AT, and MC which their value is present, absent, and mc respectively. The outside of the table, there is a submit button. Teacher needs to click which attendance of the student, either pt, at or mc. Teacher needs to click for all students. When submit button clicked by teacher, attendance of the students will be inserted into the database. When I execute code below, there nothing value is inserted in the database. Can some please help me to figure it out where i went wrong? I worked on this code for past 2 weeks yet still not getting an expected result. Thank you.

$getdata = mysql_query("select * from student where 
     class = '$class' order by name ")     or die(mysql_query);

if (isset($_POST['submit'])){
$id = 1;
while($row = mysql_fetch_assoc($getdata)){
    if(isset($_POST['a'.$id])) {
        $status = $_POST['a'.$id];      
        if(!empty($status)){
            if(($status == "present" || $status == "mc")){
                $attend = 1;
            }
            else if($status == "absent"){
                $attend = 0;
            }
            $query = "INSERT INTO attendance(birth_no, date, status, attend) 
            VALUES ('$birth_no','$date','$status','$attend')";
            if($query_run = mysql_query($query)){
                echo 'Insert attendance done';
            }
            else{
                echo'Attendance not inserted.';
            }                   
        }
        else{
            echo 'Please enter all fields';
        }
    }
    $id ++;
}
}
else {
?>   
<form action="addattend.php" method = "POST">
<table>
<?php
$id = 1;
while($row = mysql_fetch_assoc($getdata)){
    $birth_no= $row['birth_no'];
    $name = $row['name'];
?>
    <tr>
        <td><center><?php echo $id ?></center></td>
        <td><center><?php echo $date ?></center></td>
        <td><center><?php echo $birth_no ?></center></td>
        <td><center><?php echo $name ?></center></td>
        <td>
            <input type="radio" name="a<?php echo $id; ?>" value="present">PT
            <input type="radio" name="a<?php echo $id; ?>" value="absent">AT
            <input type="radio" name="a<?php echo $id; ?>" value="mc">MC
        </td>
    </tr>
<?php
    $id ++;
} // end while
?>
</table>
<center><input type="submit" name="submit" value="Submit"></center>
</form> <!-- end the form here -->
<?php    
} // end else
?>

this is what i meant about the interface(picture below)

enter image description here

for every row, there are 3 radio buttons. The list of student was retrieved from the database. Teachers need to select the attendance for all students. with a single submit button, All the attendance will be inserted in the database with birth_no, date(selected earlier by teacher) and also $attend which is assigned after attendance selected.

jenny
  • 61
  • 3
  • 9
  • Whats the error?Does it say Attendance not inserted?Show the connection part. – Mihai Sep 11 '13 at 12:36
  • Where are you defining `$birth_no` and `$date` for the values of your insert? If they are not defined properly that may be your problem. And you should do a foreach loop instead of a while loop and use the primary key instead of `$id`. Oh and stop using [`mysql_*` functions as they are deprecated.](http://stackoverflow.com/a/13944958/1707323) – amaster Sep 11 '13 at 12:43
  • @Mihai there's no error message displayed. – jenny Sep 11 '13 at 13:31
  • @amaster507 my primary key is birth_no. I don't get it when you said use primary key instead of $id. – jenny Sep 11 '13 at 13:34

1 Answers1

0

I don't get it when you said use primary key instead of $id

Let me explain: When you use the while loop with $id you increment the $id for each while loop. What if your table is missing the primary key '1' or any one for that matter? Answer: The first row may not be correlating to the primary key of '1'. Let me further explain with some data:

An example table:

student_id | name
-----------+--------
1          | John
3          | Jane
4          | Bob

now lets loop through with a while using sudo code

$id = 1;
while(row exists){
    print row['name']." has primary key of $id";
    $id++;
}

This would print something similar to:

John has primary key of 1
Jane has primary key of 2
Bob has primary key of 3

Which would be incorrect.

Here is how I would go about doing something like this, this code may need slight adaptation for your specific use:

<?php

$getdata = mysql_query("select * from student where 
     class = '$class' order by name ")     or die(mysql_query);

if (isset($_POST['submit'])){
while($row = mysql_fetch_assoc($getdata)){
    //set the id equal to the primary key
    $id = $row["birth_no"];
    $date = date("Y-m-d");
    if(isset($_POST['a'.$id])) {
        $status = $_POST['a'.$id];      
        if(!empty($status)){
            if(($status == "present" || $status == "mc")){
                $attend = 1;
            }
            else {
                $attend = 0;
            }
            $query = "INSERT INTO attendance(birth_no, date, status, attend) 
            VALUES ('$id','$date','$status','$attend')";
            if($query_run = mysql_query($query)){
                echo 'Insert attendance done';
            }
            else{
                echo'Attendance not inserted.';
            }                   
        }
        else{
            echo 'Please enter all fields';
        }
    }
}
}
else {
?>   
<form action="addattend.php" method = "POST">
<table>
<?php
while($row = mysql_fetch_assoc($getdata)){
    $birth_no = $row['birth_no'];
    $name = $row['name'];
?>
    <tr>
        <td><center><?php echo date("F j, Y") ?></center></td>
        <td><center><?php echo $birth_no ?></center></td>
        <td><center><?php echo $name ?></center></td>
        <td>
            <input type="radio" name="a<?php echo $birth_no; ?>" value="present">PT
            <input type="radio" name="a<?php echo $birth_no; ?>" value="absent">AT
            <input type="radio" name="a<?php echo $birth_no; ?>" value="mc">MC
        </td>
    </tr>
<?php
} // end while
?>
</table>
<center><input type="submit" name="submit" value="Submit"></center>
</form> <!-- end the form here -->
<?php    
} // end else
?>
amaster
  • 1,915
  • 5
  • 25
  • 51
  • @jenny this would also prevent an error if a new student was added between getting the data for the form and submitting the form which would cause errors and incorrect data in your original code. – amaster Sep 11 '13 at 16:57