【发布时间】:2021-02-25 13:45:03
【问题描述】:
我正在为一家理发店建立预约系统。 它的工作原理是这样的:
- PHP 中的约会页面,客户将在其中添加他们的姓名、电话号码、他们想要的东西、日期和时间(例如下午 2 点)。
- 这会将这些信息添加到数据库中
- 管理页面将从数据库中获取此数据并向理发师显示预约
我的问题:假设理发师的工作时间是上午 8 点和晚上 9 点。一位客户想要上午 8 点预约,而下一位预约的客户不应该看到上午 8 点的“空间”可用,只有上午 9 点和之后的时间。
我该怎么做?我想我会编写一个代码,从数据库中读取到那天的所有数据并获取所有花费的时间。
预订表格:
<!DOCTYPE html>
<html>
<head>
<title>
Agendar
</title>
<link href="theme.css" rel="stylesheet">
</script>
</head>
<body>
<?php
// (A) PROCESS RESERVATION
if (isset($_POST['date'])) {
require "reserve.php";
if ($_RSV->save(
$_POST['date'], $_POST['slot'], $_POST['name'], $_POST['email'],
$_POST['tel'], $_POST['notes'])) {
echo "<div class='ok'>Reserva feita! Entraremos em contato caso haja necessidade.</div>";
} else { echo "<div class='err'>".$_RSV->error."</div>"; }
}
?>
<!-- (B) RESERVATION FORM -->
<h1>Agendar um horário</h1>
<form id="resForm" method="post" target="_self">
<label for="res_name">Nome</label>
<input type="text" required name="name" placeholder="Seu nome"/>
<label for="res_tel">Telefone</label>
<input type="text" required name="tel" placeholder="(12) 12345-6789"/>
<label for="res_notes">Barba, cabelo ou os dois?</label>
<select required name="notes">
<option disabled selected value> -- Selecione -- </option>
<option value="Barba">Barba</option>
<option value="Cabelo">Cabelo</option>
<option value="Cabelo e Barba">Cabelo e Barba</option>
</select>
<label>Date</label>
<input type="date" required id="res_date" name="date" placeholder="<?=date("Y-m-d")?>">
<label>Horário</label>
<select required name="slot">
<option disabled selected value> -- Selecione -- </option>
<option value="8:00">8:00</option>
<option value="9:00">9:00</option>
</select>
<input type="submit" value="Reservar"/>
</form>
</body>
</html>
管理页面:
<?php
include_once 'database.php';
$result = mysqli_query($conn,"SELECT * FROM reservations");
?>
<!DOCTYPE html>
<html>
<head>
<link href="styles.css" rel="stylesheet">
<title> Agendamentos</title>
</head>
<body>
<?php
if (mysqli_num_rows($result) > 0) {
?>
<table>
<tr>
<td>ID</td>
<td>Data</td>
<td>Horário</td>
<td>Nome</td>
<td>Telefone</td>
<td>Vai fazer</td>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["res_id"]; ?></td>
<td><?php echo $row["res_date"]; ?></td>
<td><?php echo $row["res_slot"]; ?></td>
<td><?php echo $row["res_name"]; ?></td>
<td><?php echo $row["res_tel"]; ?></td>
<td><?php echo $row["res_notes"]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
<?php
}
else{
echo "No result found";
}
?>
</body>
</html>
结构:
CREATE TABLE `reservations` (
`res_id` int(11) NOT NULL AUTO_INCREMENT,
`res_date` date DEFAULT NULL,
`res_slot` varchar(32) DEFAULT NULL,
`res_name` varchar(255) NOT NULL,
`res_tel` varchar(60) NOT NULL,
`res_notes` text DEFAULT NULL,
`res_email` varchar(255) DEFAULT NULL,
PRIMARY KEY (`res_id`),
KEY `res_date` (`res_date`),
KEY `res_slot` (`res_slot`),
KEY `res_name` (`res_name`),
KEY `res_tel` (`res_tel`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin
【问题讨论】:
-
所有约会都是1小时吗?
-
我做了 1 小时,但我猜理发师会预约 30 分钟
-
最简单的解决方案:在约会表中添加
is_busy列。更好的是,添加多对多关系表来连接客户和约会。 -
你能分享一个示例代码吗?我是 php 和 db 的新手
-
这对我来说没问题,但是下次当有人问你 structure 时,只需在你的 PhpMyAdmin 中运行这个 SQL 查询:
show create table reservations,而不是在响应选项中选择 @987654326 @并复制代码
标签: php html mysql database mariadb