我打算用php学习一个小型的webapp。我面临有关要执行的SQL查询的问题。 基本上,我必须根据日期和时间来管理预订。我已经用类型=日期和类型=时间的形式将这两件事分开了。
日期为“数据”。
小时是“ orario_inizio”。
这是我的表格:
Data: <br><input type="date" name="data" ><br>
Orario Inizio: <br><input type="time" name="orario_inizio" min="09:30:00" max="16:30:00"><br>
Orario Fine: <br><input type="time" name="orario_fine" min="10:00:00" max="18:30:00"><br>
为了在数据库中输入预订,必须有“ orario_inizio ”两个小时的空间,即在接下来的两个小时中没有其他预订。
input.php
// Richiedente
$nominativo = $_POST['nominativo'];
$email = $_POST['email'];
$oggetto = $_POST['oggetto'];
$data = $_POST['data'];
$orario_inizio = $_POST['orario_inizio'];
$orario_fine = $_POST['orario_fine'];
//inserting data order
$query1 = "INSERT INTO Prenotazione (nominativo,email,data,orario_inizio,orario_fine,oggetto,nominativoi,emaili,nominativoe,emaile) VALUES ('$nominativo','$email','$data','$orario_inizio','$orario_fine','$oggetto','$nominativoi','$emaili','$nominativoe','$emaile')"; $result1 = mysqli_query($conn, $query1 );
$result1 = mysqli_query($conn, $query1 );
?>
我无法理解的是如何开发一个查询,使我能够考虑用户选择的日期,即两个小时内没有预订。让我用一个例子来解释: user1希望每天9月24日上午10:30进行预订。我必须确保在12:30之前没有保留。如果还没有其他预订,那么如果我发送警报,我想输入此预订。
我已经完成了第二部分,即插入数据库,但是我不知道如何进行第一次检查。
$query1 = "INSERT INTO Prenotazione (nominativo,email,data,orario_inizio,orario_fine,oggetto,nominativoi,emaili,nominativoe,emaile) VALUES ('$nominativo','$email','$data','$orario_inizio','$orario_fine','$oggetto','$nominativoi','$emaili','$nominativoe','$emaile')";
我已阅读到可能使用SUBDATE
你能帮我吗?谢谢
更新。
表Prenotazione
答案 0 :(得分:0)
请考虑以下内容:
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table(booking_id SERIAL PRIMARY KEY,booking_start INT NOT NULL);
INSERT INTO my_table (booking_start) VALUES (3),(7),(10);
SET @n = 9;
INSERT INTO my_table (booking_start)
SELECT @n
FROM (SELECT 1) x
LEFT
JOIN my_table y
ON y.booking_start BETWEEN @n AND @n+2
WHERE y.booking_start IS NULL
LIMIT 1;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
SELECT * FROM my_table;
+------------+---------------+
| booking_id | booking_start |
+------------+---------------+
| 1 | 3 |
| 2 | 7 |
| 3 | 10 |
+------------+---------------+
3 rows in set (0.00 sec)
-- Nothing happens, because 10 is between 9 and 11.
SET @n = 8;
INSERT INTO my_table (booking_start)
SELECT @n
FROM (SELECT 1) x
LEFT
JOIN my_table y
ON y.booking_start BETWEEN @n AND @n+2
WHERE y.booking_start IS NULL
LIMIT 1;
SELECT * FROM my_table;
+------------+---------------+
| booking_id | booking_start |
+------------+---------------+
| 1 | 3 |
| 2 | 7 |
| 3 | 10 |
+------------+---------------+
3 rows in set (0.00 sec)
-- Nothing happens, because 10 is between 8 and 10.
SET @n = 7;
INSERT INTO my_table (booking_start)
SELECT @n
FROM (SELECT 1) x
LEFT
JOIN my_table y
ON y.booking_start BETWEEN @n AND @n+2
WHERE y.booking_start IS NULL
LIMIT 1;
SELECT * FROM my_table;
+------------+---------------+
| booking_id | booking_start |
+------------+---------------+
| 1 | 3 |
| 2 | 7 |
| 3 | 10 |
+------------+---------------+
3 rows in set (0.00 sec)
-- Nothing happens, because 7 is between 7 and 9.
SET @n = 4;
INSERT INTO my_table (booking_start)
SELECT @n
FROM (SELECT 1) x
LEFT
JOIN my_table y
ON y.booking_start BETWEEN @n AND @n+2
WHERE y.booking_start IS NULL
LIMIT 1;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
SELECT * FROM my_table;
+------------+---------------+
| booking_id | booking_start |
+------------+---------------+
| 1 | 3 |
| 2 | 7 |
| 3 | 10 |
| 4 | 4 |
+------------+---------------+
4 rows in set (0.00 sec)
-- Success, because there is no value between 4 and 6.
答案 1 :(得分:0)
由于您未提供表架构,因此我将不得不使用占位符列名等。
正如前面的注释所解释的,您可以使用mysql函数BETWEEN
来查看在给定的时间“之间” 是否存在(对于日期也有效)。它的作用是返回一个布尔值。如果为true,则为1,否则为0。
假设您的表上有一个日期行,以便查看特定日期的预订,则可以编写以下示例:
$sql="SELECT
COUNT(column_id)
FROM
table_name
WHERE
TIME($orario_inizio) BETWEEN '10:30' AND '12:30'
AND date_column=CURDATE()";
$result_set=mysqli_query($conn, $sql);
$checkIfValid=mysqli_fetch_array($conn, $result_set)[0];
if($checkIfValid > 0) {
//throw error
}
else {
//perform insert query
}
您可以调整逻辑以适合自己的喜好。例如,CURDATE()
仅使用当前日期,但是也许您有一个可以使用的特定预订日期?
此SQL基本上根据时间在 10:30 AM和12:30 AM之间并且日期是今天的条件来计算您选择的ID(用户ID或其他) (可以是给定的预订日期)。
现在,如果此sql找到任何记录,它将被放入计数中。因此它的值为0或更高。如果计数大于0,则表明我们很忙,或者应用了逻辑,这意味着您无法在该天/时间段进行预订,因此我们将抛出错误。但是,如果计数不大于0,则继续进行INSERT
,因为一切都必须没事。