我正在尝试为跆拳道学校制定时间表,我希望管理员能够对他们的桌子进行 CRUD。
这就是我希望它看起来的样子:
这是我现在的代码:
<?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'); ?>
我很想进行一些额外的改进,但我无法弄清楚这种形式。在我添加更新功能之前,它工作正常。现在,当我在其中一行中添加一些新数据并单击更新时-它不会给我会话消息并且不会更新视图中的字段(但我认为它在数据库中)。
任何意见,将不胜感激。我想我可以用一些 AJAX 来解决这个问题 - 但我有一段时间没有碰过它,坦率地说不记得如何......