【问题标题】:Disable selected value from DataBase禁用从数据库中选择的值
【发布时间】:2021-02-25 13:45:03
【问题描述】:

我正在为一家理发店建立预约系统。 它的工作原理是这样的:

  1. PHP 中的约会页面,客户将在其中添加他们的姓名、电话号码、他们想要的东西、日期和时间(例如下午 2 点)。
  2. 这会将这些信息添加到数据库中
  3. 管理页面将从数据库中获取此数据并向理发师显示预约

我的问题:假设理发师的工作时间是上午 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


【解决方案1】:

首先,您的reservations 表不应包含nametelemail 等用户数据,而只需创建clients 表并将每个客户端存储为一行。 Next 在您的reservations 表中添加整数类型的列client_id,您将在clients 表中保留客户端的数字ID。这样您就不会重复客户的数据。您可以使用JOIN 语句在一个查询中从多个表中获取数据。这是一对一的关系(一个预订只能有一个客户)。

当你生成可用列表时,休息是相当容易的

  1. 选择指定日期的所有行,已分配客户端select * from reservations where client_id &gt; 0
  2. 在 PHP 中仅为这些时间生成约会,这在 DB 中没有行

提示:作为程序员,将小时以 24 小时格式存储在 DB 中会更容易,这样您将使用 8:0016:00 而不是 8+AM vs 8+@987654336 @。当然最后你可以在页面上显示8:00 PM

CREATE TABLE `reservations` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `res_date` date DEFAULT NULL,
  `slot` time DEFAULT NULL,
  `client_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

INSERT INTO reservations (res_date, slot, client_id) VALUES ('2021-02-25', '08:00:00', 1);
INSERT INTO reservations (res_date, slot, client_id) VALUES ('2021-02-26', '11:30:00', 2);
INSERT INTO reservations (res_date, slot, client_id) VALUES ('2021-02-27', '16:30:00', 3);

CREATE TABLE `clients` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(100) DEFAULT NULL,
  `last_name` varchar(100) DEFAULT NULL,
  `email` varchar(100) DEFAULT NULL,
  `phone` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

INSERT INTO clients (first_name, last_name, email, phone) VALUES ('John', 'Doe', 'john@doe.tld', '+1234');
INSERT INTO clients (first_name, last_name, email, phone) VALUES ('Jane', 'Doe', 'jane@doe.tld', '+4321(7)');
INSERT INTO clients (first_name, last_name, email, phone) VALUES ('Johan', 'Müller', 'mueller@foode', '+49123');

JOIN - 一次查询连接的表:

SELECT reservations.*, clients.*
FROM reservations
LEFT JOIN clients ON (reservations.client_id = clients.id)

【讨论】:

  • 太棒了!我将使用预订表向理发师显示数据,以防他需要致电客户或在 HTML 页面中查看预订。我会按照你说的创建另一个表!
  • 取决于您的需要 - 您可以通过 JOIN 查询从多个表中获取数据和/或为每个表创建单独的查询。
  • 您所做的此编辑解决了@biesior 的“同时进行两个约会”问题?
  • 如果你会编程 - 我会的。这里只是一个 JOIN 查询示例,您将如何使用它。
【解决方案2】:

您必须检查可用时间。

  • 在预订页面生成选择框之前,您可以执行选择查询以检查理发师是否可用!我建议您在这部分使用24小时格式。

1- 创建 8:00 到 21:00 之间的时间的函数


    function dailyTime(){
        $time = [];
        $start = 8;
        $end = 21;
        for ($time = $start; $time <= $end; $time++) {
         array_push($time,date("H:00", mktime($time+1)));
        }
        return $time;
    }


2- 对选项进行过滤:

a) 生成 8 - 21 之间的时间 b) 从数组中取消设置保留时间 c) 为时间数组中剩余的项目制作选择框


          <select required name="slot">
            <option disabled selected value> -- Selecione -- </option>
            <?php
            $availableTimes = dailyTime();
            $result = mysqli_query($conn,"SELECT * FROM reservations");
            while($row = mysqli_fetch_array($result)) {
                if (($key = array_search($row['res_slot'], $availableTimes)) !== false) {
                    unset($availableTimes[$key]);
                }
            }
            foreach($availableTimes as $free){
                    echo '<option>'.$free.'<option>';
            }
            
            ?>
          </select>

【讨论】:

  • 我试过你的代码 @milad-elyasi 但它没有生成小时数
猜你喜欢
  • 1970-01-01
  • 2017-01-17
  • 2013-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-16
  • 2017-11-04
相关资源
最近更新 更多