Help with submitting records into a database.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shuffweb
Forum Newbie
Posts: 5
Joined: Fri Apr 11, 2008 11:28 am

Help with submitting records into a database.

Post by shuffweb »

Hi everyone, Im looking for a bit of help concerning a php problem ive stumbled upon.

Scenario: For my final year project at uni I have to create an online attendance register system using PHP & MySQL which allows a lecturer to;

1) login ->
2) view the modules they teach (modules.php)->
3) view a list of timetabled sessions for the chosen module (Labs, Tutorials, Lectures etc...) (session.php)->
4) view a class list for the chosen session, containing a list of all the students timetabled for the session (student_session.php) -->
5)submit a form which inserts the relevant values (student_id, session_id, week_no, attendance[0 or 1]) into a 'student_session' table.

Problem:So far I have completed tasks 1-4 without any problems, however I am considerably stuck on task 5.

Below is the relevant part of 'student_session.php', which displays a new row for every timetabled student, along with a check box to mark their attendance, and then passes it onto the form handler (submit_attendance.php)

Code: Select all

 
<?php   
        include ('./includes/db_connect.php');
                                        
                    $query  = 
                    "SELECT  CONCAT_WS(' ',student.first_name, student.last_name) AS name, student.student_id, student.course_id, module.module_id, module.course_id
                    FROM    student, module
                    WHERE student.course_id = module.course_id 
                    AND module.module_id = '{$_SESSION['module_id']}'
                    ORDER BY name ASC ";
                                
                    $result = mysql_query($query);
                    $num = mysql_num_rows($result);                 
 
                    // If it ran OK, display the records.
                    if ($num > 0) 
                    
                    { 
                        echo 
                            "<p>There are <strong>$num</strong> students timetabled to attend this session.</p>\n"
                            
                        ;
                        
 
                        // Table header.
                        echo 
                            '<form method="post" action="submit_attendance.php">
                            <table class="modules" align="left" cellspacing="0" cellpadding="5" >
                                <tr>
                                    <td class="moduletitle" colspan="2">Tick the box to record a students attendance</td>
                                </tr>
                        
                                <tr class="hover">
                                    <td class="module" align="left"><b>Name</b></td>
                                    <td class="module" align="left"><b>Present</b></td>
                                </tr>'
                        ;
                    // Fetch and print all the records.
                    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
                    
                        {
                            echo 
                                '<tr class="module" align="left">
                                    <td class="module" align="left">
                                        <a href="./student_details.php? student_id=' . $row['student_id'] .'">' . $row['name'] .'</a>
                                        <input type="hidden" name="student[]"  />
                                    </td>
                                    
                                    <td class="module" align="left">
                                        
 
                                            <input type="checkbox" name="attendance[]" value="1" >
                                                <img src="./images/green_tick.png" width="12px" height="12px"/>
                                            <br>
                                        
                                    </td>
                                    
                                        
                                </tr>';
                            
                        }
                            echo 
                                '<tr>
                                    <td>
                                    
                                    <input type="submit" class="submit"  name="submit" value="Submit" />
                                    <input type="hidden" name="submitted" value="TRUE" />
                                    </td>
                                </tr>
                                </table></form><br/>'
                            ;
                            
                    // Free up the resources.                       
                    mysql_free_result ($result); 
 
                    } 
                    
                    else // If it did not run OK.
                    { 
                        echo
                            '<p class="error">No students are timetabled to attend this session.</p>'
                        ;
                    }
        
                
                    ?>
 
submit_attendance.php - This is where the problem occurs, the desired result would be to enter the values of the $_SESSION['session_id'] and $_SESSION['week_no'] (to represent the teaching session: for later use to generate reports)
and then the names of every student who was present.
However all it does at present is enter one row into the 'student_session' table with null values for all fields except a '1' in the 'attendance' field.

Code: Select all

 
        <?php
            session_start();
        
                        include ('./includes/db_connect.php');                                                  
                    
                    
                    
                    
                    if (isset($_POST['submitted'])) { // Check if the form has been submitted.
                    
                    
                    $a = isset( $_POST['attendance'] ) ? 1 : 0;
    
    
        if (isset($_POST['student'])) {
            $student = escape_data($_POST['student']);
        } else {
            $student = NULL;
        }
 
        if (isset($_SESSION['week_id'])) {
            $week = escape_data($_SESSION['week_id']);
        } else {
            $week = NULL;
        }
        
        if (isset($_SESSION['session_id'])) {
            $sess = escape_data($_SESSION['session_id']);
        } else {
            $sess = NULL;
        }
 
    
            
        
        }
        
        if ($a && $student && $week && $sess) { // If everything's OK.
    
        // Add the student_session to the database.
        $query = "INSERT INTO student_session (attendance, student_id, week_id, session_id) VALUES ('$a', '$student', '$week', '$sess')";
        if ($result = mysql_query ($query)) { // Worked.
            echo '<p>The attendance has been recorded.</p>';
        } else { // If the query did not run OK.
            echo '<p><font color="red">Your submission could not be processed due to a system error.</font></p>'; 
        }
        
        }
    
 
    
    ?>
 
I know im nearly there, and im more than certain its just a case of a missing for loop or an array,
sadly im more of a front end developer and I would really appreciate it if some kind PHP/MySQL guru would give me a hint or a push in the right direction. :)

Thanks in advance - Dan
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: Help with submitting records into a database.

Post by kryles »

Have you used echo to print the query on the screen to see if it is what you expect? If not, then check your POST variables are using the right names
Post Reply