0

Hi,

I need help with the attendance form (index.php). When I tried to update the first row, nothing happened. For the last row, the message says, "We couldn't update the attendance form at this time."

Please check lines 96 and 101 to see if got any error.

And also I need help with the healthcheck form (healthcheck.php). The tabs (healthcheck, illness, and remarks) are not working when I tried to click. And also when I tried to update the data, nothing happened.

The php files are using bootstrap.

Download the attendance from zip file: https://drive.google.com/file/d/0B07_pOHhTox3ZTBoTHF1VlpIMVU/view?usp=sharing
Inside there is the database table called attendance.sql. You need xampp and you must import the table (attendance.sql) in phpmyadmin.

3
Contributors
11
Replies
55
Views
1 Year
Discussion Span
Last Post by hielo
0

index.php

<!DOCTYPE html>

<?php
require 'connect2.inc.php';
?>

<html>
<head>
    <title>Attendance Form</title>
    <link rel="stylesheet" href="http://bootswatch.com/paper/bootstrap.min.css">
    <style type="text/css">

    </style>
</head>

<body>
    <nav class="navbar navbar-inverse">
    <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-2">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="index.php">Mindspace Attendance form</a>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-2">
            <ul class="nav navbar-nav">
                <li><a href="register.php">Register student</a></li>
                <li><a href="healthcheck.php">Healthcheck</a></li>
            </ul>
        </div>
    </div>
    </nav>

    <div class="row vertical-center-row" id="form-container">
        <div class="col-md-10 col-md-offset-1" style="background:white">
            <form action="index.php" id="Stu_ATT" class="form-horizontal" method="POST" action="$_SERVER['PHP_SELF']">
                <fieldset>
                <legend>Attendance</legend>

                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Student name</th>
                            <th>Present</th>
                            <th>Late</th>
                            <th>Time in</th>
                            <th>Time out</th>
                            <th>Remarks</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php while($row = mysqli_fetch_array($result))
                    {
                    ?>
                        <tr>
                            <td><?php echo $row['id']; ?></td>
                            <td><?php echo $row['Sname']; ?></td>
                            <td><input type="checkbox" name="present" id="present"<?php echo $row['present'] > 0 ? ' checked':'';?>></td>
                            <td><input type="checkbox" name="late" id="late"<?php echo $row['late'] > 0 ? ' checked':'';?>></td>
                            <td><input type="time" class="form-control" name="timeIn" id="timeIn" value="<?php echo $row['timeIn']; ?>"></td>
                            <td><input type="time" class="form-control" name="timeOut" id="timeOut" value="<?php echo $row['timeOut']; ?>"></td>
                            <td><input type="text" class="form-control" id="remarks" name="remarks" value="<?php echo $row['remarks']; ?>"></td>
                        </tr>
                    <?php } ?>
                    </tbody>
                </table>

                <div class="form-group">
                    <input type="submit" class="btn btn-primary" value="Submit">
                </div>

                </fieldset>
            </form>
        </div>
    </div>
</nav>
</body>
</html>

<?php

if (isset($_POST['present']) && isset($_POST['late']) && isset($_POST['timeIn']) && isset($_POST['timeOut']) && isset($_POST['remarks']))
{
    $present = $_POST['present'];
    $late = $_POST['late'];
    $remarks = $_POST['remarks'];
    $timeIn = $_POST['timeIn'];
    $timeOut = $_POST['timeOut'];

    // Start updating process
    if (!empty($id))
    {
        $query = "SELECT * FROM `attendance` WHERE `id`='$id'";
        $query_run = mysql_query($query);

        $query = "UPDATE `attendance` SET `present`= '".mysql_real_escape_string($present)."',`late`= '".mysql_real_escape_string($late)."', `timeIn`= '".mysql_real_escape_string($timeIn).";, `timeOut`= '".mysql_real_escape_string($timeOut).";, `remarks`= '".mysql_real_escape_string($remarks)."' WHERE `id` = '$id' ";
        if ($query_run = mysql_query($query))
        {
            echo 'Updated successfully.';
        }
        else
        {
            echo 'Sorry, we couldn\'t update at this time. Try again later.';
        }
    }
    else
    {
        echo 'We couldn\'t update the attendance form at this time.';
    }
}
?>

healthcheck.php

<!DOCTYPE html>

<?php
require 'connect3.inc.php';
?>

<html>
<head>
    <title>Healthcheck & Remarks</title>
    <link rel="stylesheet" href="http://bootswatch.com/paper/bootstrap.min.css">
    <style type="text/css">

    </style>
</head>

<body>
    <nav class="navbar navbar-inverse">
    <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-2">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="index.php">Mindspace Attendance form</a>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-2">
            <ul class="nav navbar-nav">
                <li><a href="register.php">Register student</a></li>
                <li class="active"><a href="healthcheck.php">Healthcheck</a></li>
            </ul>
        </div>
    </div>
    </nav>

    <div class="row vertical-center-row" id="form-container">
        <div class="col-md-10 col-md-offset-1" style="background:white">
            <ul class="nav nav-tabs">
                <li class="active"><a href="#home" data-toggle="tab">Healthcheck</a></li>
                <li><a href="#illness" data-toggle="tab">Illness</a></li>
                <li><a href="#remarks" data-toggle="tab">Remarks</a></li>
            </ul>

            <div class="tab-content">
                <div class="tab-pane fade in active" id="home">
                    <br>
                    <form action="healthcheck.php" id="Stu_HLTH" class="form-horizontal" method="POST" action="$_SERVER['PHP_SELF']">
                        <fieldset>
                        <legend>Healthcheck</legend>

                        <table class="table table-striped table-hover">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Student name</th>
                                    <th>Eyes</th>
                                    <th>Respiratory</th>
                                    <th>Oral</th>
                                    <th>Hands</th>
                                    <th>Legs/Feet</th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php while($row = mysqli_fetch_array($result))
                            {
                            ?>
                                <tr>
                                    <td><?php echo $row['id']; ?></td>
                                    <td><?php echo $row['Sname']; ?></td>
                                    <td><input type="text" class="form-control" id="eyes" name="eyes" value="<?php echo $row['eyes']; ?>"></td>
                                    <td><input type="text" class="form-control" id="respiratory" name="respiratory" value="<?php echo $row['respiratory']; ?>"></td>
                                    <td><input type="text" class="form-control" id="oral" name="oral" value="<?php echo $row['oral']; ?>"></td>
                                    <td><input type="text" class="form-control" id="hands" name="hands" value="<?php echo $row['hands']; ?>"></td>
                                    <td><input type="text" class="form-control" id="legs_feet" name="legs_feet" value="<?php echo $row['legs_feet']; ?>"></td>
                                </tr>
                            <?php } ?>
                            </tbody>
                        </table>

                        <div class="form-group">
                            <input type="submit" class="btn btn-primary" value="Submit">
                        </div>
                        </fieldset>
                    </form>
                </div>

                <div class="tab-pane fade" id="illness">
                    <br>
                    <form action="healthcheck.php" id="Stu_ILL" class="form-horizontal" method="POST" action="$_SERVER['PHP_SELF']">
                        <fieldset>
                        <legend>Illness</legend>

                        <table class="table table-striped table-hover">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Student name</th>
                                    <th>Illness</th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php while($row = mysqli_fetch_array($result))
                            {
                            ?>
                                <tr>
                                    <td><?php echo $row['id']; ?></td>
                                    <td><?php echo $row['Sname']; ?></td>
                                    <td><input type="text" class="form-control" id="illness" name="illness" value="<?php echo $row['illness']; ?>"></td>
                                </tr>
                            <?php } ?>
                            </tbody>
                        </table>

                        <div class="form-group">
                            <input type="submit" class="btn btn-success" value="Submit">
                        </div>
                        </fieldset>
                    </form>
                </div>

                <div class="tab-pane fade" id="remarks">
                    <br>
                    <form action="healthcheck.php" id="Stu_RMRK" class="form-horizontal" method="POST" action="$_SERVER['PHP_SELF']">
                        <fieldset>
                        <legend>Remarks</legend>

                        <table class="table table-striped table-hover">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Student name</th>
                                    <th>Remarks</th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php while($row = mysqli_fetch_array($result))
                            {
                            ?>
                                <tr>
                                    <td><?php echo $row['id']; ?></td>
                                    <td><?php echo $row['Sname']; ?></td>
                                    <td><input type="text" class="form-control" id="remarks" name="remarks" value="<?php echo $row['remarks']; ?>"></td>
                                </tr>
                            <?php } ?>
                            </tbody>
                        </table>

                        <div class="form-group">
                            <input type="submit" class="btn btn-info" value="Submit">
                        </div>
                        </fieldset>
                    </form>
                </div>
            </div>

        </div>
    </div>

</body>

</html>

<?php

if (isset($_POST['eyes']) && isset($_POST['respiratory']) && isset($_POST['oral']) && isset($_POST['hands']) && isset($_POST['legs_feet']) && isset($_POST['illness']) && isset($_POST['remarks']))
{
    $eyes = $_POST['eyes'];
    $respiratory = $_POST['respiratory'];
    $oral = $_POST['oral'];
    $hands = $_POST['hands'];
    $legs_feet = $_POST['legs_feet'];
    $illness = $_POST['illness'];
    $remarks = $_POST['remarks'];

    // Start updating process
    if (!empty($id))
    {
        $query = "SELECT * FROM `healthcheck` WHERE `id`='$id'";
        $query_run = mysql_query($query);

        $query = "UPDATE `healthcheck` SET `eyes`= '".mysql_real_escape_string($eyes)."',`respiratory`= '".mysql_real_escape_string($respiratory)."', `oral`= '".mysql_real_escape_string($oral).";, `hands`= '".mysql_real_escape_string($hands).";, `legs_feet`= '".mysql_real_escape_string($legs_feet).";, `illness`= '".mysql_real_escape_string($illness).";, `remarks`= '".mysql_real_escape_string($remarks)."' WHERE `id` = '$id' ";
        if ($query_run = mysql_query($query))
        {
            echo 'Updated successfully.';
        }
        else
        {
            echo 'Sorry, we couldn\'t update at this time. Try again later.';
        }
    }
    else
    {
        echo 'We couldn\'t update the attendance form at this time.';
    }
}

?>
0

The $id variable is undefined, at line 96 you check if it's empty but you're not defining it anywhere, so the condition will always be false and you will get the else statement at line 113, the same happens in the healthcheck file at lines 178 and 193.

0

Read through the comments of the script below. Notice that the underlying html markup has changed.

<!DOCTYPE html>
<?php
require 'connect2.inc.php';

$errors=Array();

// Now there is student_id hidden field in the first column.  It should correspond to the unique id 
// record on your table.
if( array_key_exists('student_id',$_POST) )
{
    // Notice that the name of the hidden field is "student_id[]" (with brackets at the end).  This
    // way PHP "sees" an array of student_id fields.
    foreach($_POST['student_id'] as $id)
    {
        $id = intval($id);

        // The corresponding fields for a given row also have the student_id. So, instead of 
        // name="present", you now have name="present[X]" where X is the actual student_id for that
        // row.  If this is not clear, look at the browser's source code and you inspect one of the
        // rows.  Look at what's generated for student_id and how that field's value is 
        // "tied/related" to the other fields on that row.
        if(  array_key_exists('timeIn',$_POST) && array_key_exists('timeOut',$_POST) && array_key_exists('remarks',$_POST) && isset($_POST['timeIn'][$id]) && isset($_POST['timeOut'][$id]) && isset($_POST['remarks'][$id]) )
        {
            if (!empty($id))
            {
                // query to see if record exist on db
                $query = "SELECT COUNT(*) as `total` FROM `attendance` WHERE `id`='$id'";
                $query_run = mysql_query($query);
                if(!$query_run)
                {
                    echo 'Error ',__LINE__,': Sorry, we couldn\'t update at this time. Try again later.';
                }
                else
                {
                    // retrieve the query result
                    $row = mysql_fetch_assoc($query_run);

                    if( 0===intval($row['total']) )
                    {
                        $errors[$id] = 'Error ' . __LINE__ . ': There is no matching record for StudentID '.$id.'.';
                    }
                    elseif( 1===intval($row['total']) )
                    {
                        $present = (int)(array_key_exists('present',$_POST) && array_key_exists($id, $_POST['present']));
                        $late    = (int)(array_key_exists('late',$_POST) && array_key_exists($id, $_POST['late']));
                        $remarks = $_POST['remarks'][$id];
                        $timeIn  = $_POST['timeIn'][$id];
                        $timeOut = $_POST['timeOut'][$id];

                        $query = "UPDATE `attendance` SET `present`= '".$present."',`late`= '".$late."', `timeIn`= '".mysql_real_escape_string($timeIn)."', `timeOut`= '".mysql_real_escape_string($timeOut)."', `remarks`= '".mysql_real_escape_string($remarks)."' WHERE `id` = '$id' LIMIT 1";

                        if ($query_run = mysql_query($query))
                        {
                            $errors[$id] = 'StudentID ' . $id . ': Updated successfully.';
                        }
                        else
                        {
                            $errors[$id] = 'Error ' . __LINE__ . ': Sorry, we couldn\'t update at this time. Try again later.';
                        }
                    }
                    else
                    {
                        $errors[$id] = 'Error ' . __LINE__ . ': Unable to locate unique record.';
                    }
                }
            }
            else
            {
                $errors[$id] = 'We couldn\'t update the attendance form at this time.';
            }
        }
    }
}
?>
<html>
<head>
    <title>Attendance Form</title>
    <link rel="stylesheet" href="http://bootswatch.com/paper/bootstrap.min.css">
    <style type="text/css">
    </style>
</head>
<body>
    <nav class="navbar navbar-inverse">
    <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-2">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="index.php">Mindspace Attendance form</a>
        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-2">
            <ul class="nav navbar-nav">
                <li><a href="register.php">Register student</a></li>
                <li><a href="healthcheck.php">Healthcheck</a></li>
            </ul>
        </div>
    </div>
    </nav>
    <div class="row vertical-center-row" id="form-container">
        <div class="col-md-10 col-md-offset-1" style="background:white">
            <form action="index.php" id="Stu_ATT" class="form-horizontal" method="POST" action="$_SERVER['PHP_SELF']">
                <fieldset>
                <legend>Attendance</legend>
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Student name</th>
                            <th>Present</th>
                            <th>Late</th>
                            <th>Time in</th>
                            <th>Time out</th>
                            <th>Remarks</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                    // on your original file I don't see where you submitted a query to initialize 
                    // $result. I am guessing you are doing that in connect2.inc.php.  If that is
                    // the case, you may want to move that query right befoere the following while
                    // so that the table reflects the updated records.
                    while($row = mysqli_fetch_array($result))
                    {
                    ?>
                        <tr>
                            <td><?php echo $row['id']; ?><input type="hidden" name="student_id[]" value="<?php echo $row['id']; ?>"/></td>
                            <td><?php echo htmlentities($row['Sname'],ENT_QUOTES,"UTF-8"); ?></td>
                            <td><input type="checkbox"                  name="present[<?php echo $row['id']; ?>]" id="present"  <?php echo $row['present'] > 0 ? ' checked':'';?>></td>
                            <td><input type="checkbox"                  name="late[<?php echo $row['id']; ?>]"    id="late"     <?php echo $row['late'] > 0 ? ' checked':'';?>></td>
                            <td><input type="time" class="form-control" name="timeIn[<?php echo $row['id']; ?>]"  id="timeIn"   value="<?php echo $row['timeIn']; ?>"></td>
                            <td><input type="time" class="form-control" name="timeOut[<?php echo $row['id']; ?>]" id="timeOut"  value="<?php echo $row['timeOut']; ?>"></td>
                            <td><input type="text" class="form-control" name="remarks[<?php echo $row['id']; ?>]" id="remarks"  value="<?php echo $row['remarks']; ?>"></td>
                        </tr>
                    <?php } ?>
                    </tbody>
                </table>
                <div class="form-group">
                    <input type="submit" class="btn btn-primary" value="Submit">
                </div>
                </fieldset>
            </form>
        </div>
    </div>
</nav>
</body>
</html>
0

@hielo, you need to download the zip file from my first post on top

0

I suggest that you use only one db connection file:

<?php
// connect-db.php
if(!isset($mysql_db))
{
  die('You forgot to define $mysql_db first.');
}

$conn_error = 'Could not connect.';

$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';

$link = mysqli_connect($mysql_host, $mysql_user, $mysql_pass, $mysql_db);

if (!$link) 
{
    die($conn_error);
}
?>

Then index.php would be:

<?php
# specify your table first
$mysql_db = 'attendance';

# include your db connection file
require 'connect-db.php';

#if it makes it here, then the connection was successful.
# now do the query for this file
$result = mysqli_query($link, "SELECT * FROM `$mysql_db`");


$errors=Array();

// Now there is student_id hidden field in the first column.  It should correspond to the unique id 
// record on your table.
if( array_key_exists('student_id',$_POST) )
{
    // Notice that the name of the hidden field is "student_id[]" (with brackets at the end).  This
    // way PHP "sees" an array of student_id fields.
    foreach($_POST['student_id'] as $id)
    {
        $id = intval($id);
        // The corresponding fields for a given row also have the student_id. So, instead of 
        // name="present", you now have name="present[X]" where X is the actual student_id for that
        // row.  If this is not clear, look at the browser's source code and you inspect one of the
        // rows.  Look at what's generated for student_id and how that field's value is 
        // "tied/related" to the other fields on that row.
        if(  array_key_exists('timeIn',$_POST) && array_key_exists('timeOut',$_POST) && array_key_exists('remarks',$_POST) && isset($_POST['timeIn'][$id]) && isset($_POST['timeOut'][$id]) && isset($_POST['remarks'][$id]) )
        {
            if (!empty($id))
            {
                // query to see if record exist on db
                $query = "SELECT COUNT(*) as `total` FROM `attendance` WHERE `id`='$id'";
                $query_run = mysql_query($query);
                if(!$query_run)
                {
                    echo 'Error ',__LINE__,': Sorry, we couldn\'t update at this time. Try again later.';
                }
                else
                {
                    // retrieve the query result
                    $row = mysql_fetch_assoc($query_run);
                    if( 0===intval($row['total']) )
                    {
                        $errors[$id] = 'Error ' . __LINE__ . ': There is no matching record for StudentID '.$id.'.';
                    }
                    elseif( 1===intval($row['total']) )
                    {
                        $present = (int)(array_key_exists('present',$_POST) && array_key_exists($id, $_POST['present']));
                        $late    = (int)(array_key_exists('late',$_POST) && array_key_exists($id, $_POST['late']));
                        $remarks = $_POST['remarks'][$id];
                        $timeIn  = $_POST['timeIn'][$id];
                        $timeOut = $_POST['timeOut'][$id];
                        $query = "UPDATE `attendance` SET `present`= '".$present."',`late`= '".$late."', `timeIn`= '".mysql_real_escape_string($timeIn)."', `timeOut`= '".mysql_real_escape_string($timeOut)."', `remarks`= '".mysql_real_escape_string($remarks)."' WHERE `id` = '$id' LIMIT 1";
                        if ($query_run = mysql_query($query))
                        {
                            $errors[$id] = 'StudentID ' . $id . ': Updated successfully.';
                        }
                        else
                        {
                            $errors[$id] = 'Error ' . __LINE__ . ': Sorry, we couldn\'t update at this time. Try again later.';
                        }
                    }
                    else
                    {
                        $errors[$id] = 'Error ' . __LINE__ . ': Unable to locate unique record.';
                    }
                }
            }
            else
            {
                $errors[$id] = 'We couldn\'t update the attendance form at this time.';
            }
        }
    }
}
mysqli_close($link);
?>
<!DOCTYPE html>
<html>
<head>
    <title>Attendance Form</title>
    <link rel="stylesheet" href="http://bootswatch.com/paper/bootstrap.min.css">
    <style type="text/css">
    </style>
</head>
<body>
    <nav class="navbar navbar-inverse">
    <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-2">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="index.php">Mindspace Attendance form</a>
        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-2">
            <ul class="nav navbar-nav">
                <li><a href="register.php">Register student</a></li>
                <li><a href="healthcheck.php">Healthcheck</a></li>
            </ul>
        </div>
    </div>
    </nav>
    <div class="row vertical-center-row" id="form-container">
        <div class="col-md-10 col-md-offset-1" style="background:white">
            <form action="index.php" id="Stu_ATT" class="form-horizontal" method="POST" action="$_SERVER['PHP_SELF']">
                <fieldset>
                <legend>Attendance</legend>
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Student name</th>
                            <th>Present</th>
                            <th>Late</th>
                            <th>Time in</th>
                            <th>Time out</th>
                            <th>Remarks</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                    // on your original file I don't see where you submitted a query to initialize 
                    // $result. I am guessing you are doing that in connect2.inc.php.  If that is
                    // the case, you may want to move that query right befoere the following while
                    // so that the table reflects the updated records.
                    while($row = mysqli_fetch_array($result))
                    {
                    ?>
                        <tr>
                            <td><?php echo $row['id']; ?><input type="hidden" name="student_id[]" value="<?php echo $row['id']; ?>"/></td>
                            <td><?php echo htmlentities($row['Sname'],ENT_QUOTES,"UTF-8"); ?></td>
                            <td><input type="checkbox"                  name="present[<?php echo $row['id']; ?>]" id="present"  <?php echo $row['present'] > 0 ? ' checked':'';?>></td>
                            <td><input type="checkbox"                  name="late[<?php echo $row['id']; ?>]"    id="late"     <?php echo $row['late'] > 0 ? ' checked':'';?>></td>
                            <td><input type="time" class="form-control" name="timeIn[<?php echo $row['id']; ?>]"  id="timeIn"   value="<?php echo $row['timeIn']; ?>"></td>
                            <td><input type="time" class="form-control" name="timeOut[<?php echo $row['id']; ?>]" id="timeOut"  value="<?php echo $row['timeOut']; ?>"></td>
                            <td><input type="text" class="form-control" name="remarks[<?php echo $row['id']; ?>]" id="remarks"  value="<?php echo $row['remarks']; ?>"></td>
                        </tr>
                    <?php } ?>
                    </tbody>
                </table>
                <div class="form-group">
                    <input type="submit" class="btn btn-primary" value="Submit">
                </div>
                </fieldset>
            </form>
        </div>
    </div>
</nav>
</body>
</html>

Edited by hielo

0

Update: On my last post, move line 78 to the end of the file (after line 155):

...
</html>
<?php
mysqli_close($link);
0

@helio, i used ur code, but i tried to update the value and still wont work

1

Upon closer inspection I see that you are mixing mysqli_ (with an "i") with mysql_ ( without an "i") functions. To clarify, you are connecting to the database using mysqli_connect, but then you are using mysql_real_escape_string() and mysql_query(). Stick with mysqli_ since mysql_ function are now deprecated. Read the manual to see how mysqli_query() is different from mysql_query(). The same goes for the other function. The number of parameters are different, so just adding and "i" to the function call will not work.

Copy and paste the following into your index.php file. The connect-db.php file is the same I posted earlier. Read through the comments to understand what the code is doing.

<?php
# specify your table first
$mysql_db = 'attendance';

# include your db connection file
require 'connect-db.php';

# if it makes it here, then the connection was successful.  Do your select * FROM $mysql_db
# AFTER the updates (see line 87) so that your page may reflect the updated information.

# this will be used to save any potential errors
$feedback=Array();

// Now there is student_id hidden field in the first column.  It should correspond to the unique id 
// record on your table.
if( array_key_exists('student_id',$_POST) )
{
    // Notice that the name of the hidden field is "student_id[]" (with brackets at the end).  This
    // way PHP "sees" an array of student_id fields.
    foreach($_POST['student_id'] as $id)
    {
        $id = intval($id);
        // The corresponding fields for a given row also have the student_id. So, instead of 
        // name="present", you now have name="present[X]" where X is the actual student_id for that
        // row.  If this is not clear, look at the browser's source code and you inspect one of the
        // rows.  Look at what's generated for student_id and how that field's value is 
        // "tied/related" to the other fields on that row.
        if(  array_key_exists('timeIn',$_POST) && array_key_exists('timeOut',$_POST) && array_key_exists('remarks',$_POST)  )
        {
            if (!empty($id))
            {
                // query to see if record exist on db
                $query = "SELECT COUNT(*) as `total` FROM `attendance` WHERE `id`=$id";

                $query_run = mysqli_query($link,$query);
                if(!$query_run)
                {
                    echo 'Error ',__LINE__,': Sorry, we couldn\'t update at this time. Try again later.';
                }
                else
                {
                    // retrieve the query result
                    $row = mysqli_fetch_assoc($query_run);
                    mysqli_free_result($query_run);
                    if( 0===intval($row['total']) )
                    {
                        $feedback[$id] = 'Error ' . __LINE__ . ': There is no matching record for StudentID '.$id.'.';
                    }
                    elseif( 1===intval($row['total']) )
                    {
                        $present = (int)(array_key_exists('present',$_POST) && array_key_exists($id, $_POST['present']));
                        $late    = (int)(array_key_exists('late',$_POST) && array_key_exists($id, $_POST['late']));
                        $remarks = $_POST['remarks'][$id];
                        $timeIn  = $_POST['timeIn'][$id];
                        $timeOut = $_POST['timeOut'][$id];
                        $query = "UPDATE `attendance` SET `present`= '".$present."',`late`= '".$late."', `timeIn`= '".mysqli_real_escape_string($link,$timeIn)."', `timeOut`= '".mysqli_real_escape_string($link,$timeOut)."', `remarks`= '".mysqli_real_escape_string($link,$remarks)."' WHERE `id`=$id LIMIT 1";

                        $query_run = mysqli_query($link,$query);
                        if ($query_run)
                        {
                            $feedback[$id] = 'StudentID ' . $id . ': Updated successfully.';
                        }
                        else
                        {
                            $feedback[$id] = 'Error ' . __LINE__ . ': Sorry, we couldn\'t update at this time. Try again later.';
                        }
                    }
                    else
                    {
                        $feedback[$id] = 'Error ' . __LINE__ . ': Unable to locate unique record.';
                    }
                }
            }
            else
            {
                $feedback[$id] = 'We couldn\'t update the attendance form at this time.';
            }
        }
    }
}
elseif(array_key_exists('Submit',$_POST) )
{
    $feedback[]='You did not select any records for editing.';
}

# now do the query for this file
$result = mysqli_query($link, "SELECT * FROM `attendance`");
if(!$result)
{
    die( 'Line '. __LINE__ . ': ' . mysqli_error($link) );
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Attendance Form</title>
    <link rel="stylesheet" href="//bootswatch.com/paper/bootstrap.min.css" />
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <script type="text/javascript">
    function toggleRow(id_checkbox)
    {
        var tr = $(id_checkbox).closest("tr");
        $('.present,.late,.timeIn,.timeOut,.remarks',tr).each(function(){
            $(this).prop("disabled", !id_checkbox.checked );
        });
    }
    </script>
</head>
<body>
    <nav class="navbar navbar-inverse">
    <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-2">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="index.php">Mindspace Attendance form</a>
        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-2">
            <ul class="nav navbar-nav">
                <li><a href="register.php">Register student</a></li>
                <li><a href="healthcheck.php">Healthcheck</a></li>
            </ul>
        </div>
    </div>
    </nav>
    <div class="row vertical-center-row" id="form-container">
        <div class="col-md-10 col-md-offset-1" style="background:white">         
            <form id="Stu_ATT" class="form-horizontal" method="post" action="<?php echo $_SERVER['SCRIPT_NAME'];?>">
                <fieldset>
                <legend>Attendance</legend>
                <div>To edit a record, click on its corresponding ID checkbox.</div>
                <div class="errors"><?php if(!empty($feedback)){echo '<ul><li>',implode('</li><li>',$feedback),'</li></ul>';}?></div>
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Student name</th>
                            <th>Present</th>
                            <th>Late</th>
                            <th>Time in</th>
                            <th>Time out</th>
                            <th>Remarks</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                    while($row = mysqli_fetch_assoc($result))
                    {
                    ?>
                        <tr>
                            <td><input type="checkbox" name="student_id[]" value="<?php echo $row['id'];?>" onclick="toggleRow( this )"/><?php echo $row['id']; ?></td>
                            <td><?php echo htmlentities($row['Sname'],ENT_QUOTES,"UTF-8"); ?></td>
                            <td><input type="checkbox" class="present"              disabled="disabled" name="present[<?php echo $row['id']; ?>]" id="present"  <?php echo $row['present'] > 0 ? ' checked':'';?>></td>
                            <td><input type="checkbox" class="late"                 disabled="disabled" name="late[<?php echo $row['id']; ?>]"    id="late"     <?php echo $row['late'] > 0 ? ' checked':'';?>></td>
                            <td><input type="time"     class="timeIn  form-control" disabled="disabled" name="timeIn[<?php echo $row['id']; ?>]"  id="timeIn"   value="<?php echo $row['timeIn']; ?>"></td>
                            <td><input type="time"     class="timeOut form-control" disabled="disabled" name="timeOut[<?php echo $row['id']; ?>]" id="timeOut"  value="<?php echo $row['timeOut']; ?>"></td>
                            <td><input type="text"     class="remarks form-control" disabled="disabled" name="remarks[<?php echo $row['id']; ?>]" id="remarks"  value="<?php echo $row['remarks']; ?>"></td>
                        </tr>
                    <?php } ?>
                    </tbody>
                </table>
                <div class="form-group">
                    <input type="submit" name="Submit" class="btn btn-primary" value="Submit">
                </div>
                </fieldset>
            </form>
        </div>
    </div>
</nav>
</body>
</html>
<?php
mysqli_free_result($result);
mysqli_close($link);

Edited by hielo

0

@helio, the attendance form works, but what about the healthcheck form, the bootstrap navigation tabs and the updating?

Edited by SpottyBlue

0

It's hielo, not helio. As for the healthcheck form look at how I implemented the attendance form and then make an effort to implement the healthcheck form. If you have questions or get stuck then ask.

If you analyze what I did on the index.php page you will notice that I did the data processing (but only if the form was submitted) at the top first, then I began emitting the html. This is pretty much the same approach you need to take.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.