I am trying to create a schedule for a Tae Kwon Do school, and I would like the admins to be able to CRUD their table .
This is how I would like for it to look:

and here is my code right now:
<?php require_once("../../includes/initialize.php"); ?>
<?php if (!$session->is_logged_in()) { redirect_to("login.php"); } ?>
<?php
$schedules = Schedule::find_all();
?>
<?php include_layout_template('admin_header.php'); ?>
<h2>Schedule</h2>
<?php
if(isset($_POST['delete'])){
$schedule = Schedule::find_by_id($_GET['id']);
if($schedule && $schedule->delete()) {
$session->message("The line was deleted.");
redirect_to('schedule.php');
}else{
$session->message("The line could not be deleted.");
redirect_to('schedule.php');
}
}
?>
<?php
if(isset($_POST['submit'])){
$schedule = new Schedule();
$schedule->belt = $_POST['belt'];
$schedule->age = $_POST['age'];
$schedule->monday = $_POST['monday'];
$schedule->tuesday = $_POST['tuesday'];
$schedule->wednesday = $_POST['wednesday'];
$schedule->thursday = $_POST['thursday'];
$schedule->friday = $_POST['friday'];
$schedule->saturday = $_POST['saturday'];
if($schedule->save()) {
$session->message("line added successfully.");
redirect_to('schedule.php');
} else {
$message = join("<<br />", $schedule->errors);
}
}
?>
<?php
if(isset($_POST['update'])){
$schedule = Schedule::find_by_id($_POST['id']);
$schedule->belt = $_POST['belt'];
$schedule->age = $_POST['age'];
$schedule->monday = $_POST['monday'];
$schedule->tuesday = $_POST['tuesday'];
$schedule->wednesday = $_POST['wednesday'];
$schedule->thursday = $_POST['thursday'];
$schedule->friday = $_POST['friday'];
$schedule->saturday = $_POST['saturday'];
if($schedule->save()) {
$session->message("line updated successfully.");
redirect_to('schedule.php');
} else {
$message = join("<<br />", $schedule->errors);
}
}
?>
<?php echo output_message($message); ?>
<table id="schedule">
<tr>
<th>Belt</th>
<th>Age</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
<th></th>
</tr>
<?php foreach($schedules as $schedule): ?>
<tr>
<form action="schedule.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="id" value="<?php echo $schedule->id;?>" />
<td><input type="text" size="15" name="belt" value="<?php echo $schedule->belt; ?>" /></td>
<td><input type="text" size="5" name="age" value="<?php echo $schedule->age; ?>" /></td>
<td><input type="text" size="7" name="monday" value="<?php echo $schedule->monday; ?>" /></td>
<td><input type="text" size="7" name="tuesday" value="<?php echo $schedule->tuesday; ?>" /></td>
<td><input type="text" size="7" name="wednesday" value="<?php echo $schedule->wednesday; ?>" /></td>
<td><input type="text" size="7" name="thursday" value="<?php echo $schedule->thursday; ?>" /></td>
<td><input type="text" size="7" name="friday" value="<?php echo $schedule->friday; ?>" /></td>
<td><input type="text" size="7" name="saturday" value="<?php echo $schedule->saturday; ?>" /></td>
<td><input type="submit" name="update" value="Update" /><input type="submit" name="delete" value="Delete" onclick="return confirm('Are you sure you want to delete?');"/></td>
</form>
</tr>
<?php endforeach;?>
<tr>
<form action="schedule.php" enctype="multipart/form-data" method="post">
<td><input type="text" size="15" name="belt" value="" /></td>
<td><input type="text" size="5" name="age" value="" /></td>
<td><input type="text" size="7" name="monday" value="" /></td>
<td><input type="text" size="7" name="tuesday" value="" /></td>
<td><input type="text" size="7" name="wednesday" value="" /></td>
<td><input type="text" size="7" name="thursday" value="" /></td>
<td><input type="text" size="7" name="friday" value="" /></td>
<td><input type="text" size="7" name="saturday" value="" /></td>
<td><input type="submit" name="submit" value="Add new line" /></td>
</form>
</tr>
</table>
<br /><br />
<?php include_layout_template('admin_footer.php'); ?>
I would love to make some additional enhancements, but I can’t figure out this form. It was working fine up until I added the ability to update. Now when I add some new data into one of the rows, and click update – it doesn’t give me a session message and doesn’t update the field in the view (but I think it is in the DB).
Any advice would be appreciated. I imagine that I could fix this with some AJAX – but I haven’t touched it in a while and frankly don’t remember how…
The reason it doesn’t show the update on the screen is because
is above the code to update. So $schedules doesn’t get changed. I’d suggest changing
to