Hi,
I added the "dayOverview" dropdown list that needs to assign to the database, but when I test it, it is uneditable. How? Please check lines 51 and 164 - 176.
Download the attendance form zip file:
https://drive.google.com/file/d/0B07_pOHhTox3bS1mVXpiTUN3RVk/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.
index.php
<?php
# specify your table first
$mysql_db = 'attendance';
# include your db connection file
require 'connect-db.inc.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];
$dayOverview = $_POST['dayOverview'][$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)."', `dayOverview`= '".mysqli_real_escape_string($link,$dayOverview)."' 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 src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.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>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Incident report<span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="incident.php">Incident report</a></li>
<li><a href="incident_form.php">Incident form</a></li>
</ul>
</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>
<th>Day Overview</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>
<td><select class="dayOverview form-control" disabled="disabled" name="dayOverview[<?php echo $row['id']; ?>]" id="dayOverview" value="<?php echo $row['dayOverview']; ?>">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</select></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);
?>