简体   繁体   English

PHP Booking System自动更新MySQL数据库,无需任何用户交互。

[英]PHP Booking System automatic update of MySQL database, without any user interaction.

I have created a booking system for a car rental company, and I have an issue with setting the availability of cars. 我已经为一家汽车租赁公司创建了一个预订系统,但是在设置汽车可用性方面存在问题。

A brief example of how it works is that a single car, when it is booked out recieves a value of yes in a field within the database called bookedout 一个简单的例子是,当一辆车被预订后,它在数据库中的bookedout字段中获得了yes值。

Ok, so say for examples sake that there is 10 cars (this is unrealistically small but demonstrates the point) 好的,举例来说,假设有10辆汽车(虽然不切实际,但是证明了这一点)

1 of these is a Audi R8. 其中之一是奥迪R8。

Today's date is the 6th of march 2012. 今天的日期是2012年3月6日。

A user books the car from the 26th of march to the 31st of march. 用户从3月26日到3月31日预订汽车。 But they place that booking through the website today. 但是他们今天通过网站放置了该预订。 I cannot set the bookedout value to yes straight away or the website will actually consider that car unavailable from the 6th of march to the 31st of march. 我无法立即将bookedout值设置为yes ,否则网站实际上会认为该车从3月6日到3月31日不可用。

This scenario would loose a company money. 这种情况下将浪费公司资金。

So I am looking for something like a script that sits on the server, checks the current date and then checks the database every day. 因此,我正在寻找类似脚本的内容,该脚本位于服务器上,检查当前日期,然后每天检查数据库。 And for example if there is a booking due to start on today's date, it would change the value within the database so the car is unavailable to book from this day. 例如,如果要在今天的日期开始预订,它将更改数据库中的值,因此从当天起无法预订汽车。

In turn I could also adapt it to change the value back on the date on which the car is due to be returned. 反过来,我也可以对其进行修改,以在要归还汽车的日期重新更改值。

I am not specifically looking for code (although I wouldn't say no to examples), just a starting point for figuring things like this out as I have never done this before. 我并不是特别在寻找代码(尽管我不会对示例说不),这只是找出类似这样的事情的起点,因为我以前从未这样做过。

Basically, I need a way to trigger a query, without any user interaction, on a certain date. 基本上,我需要一种在特定日期无需任何用户交互即可触发查询的方法。

Any help would be seriously appreciated. 任何帮助将不胜感激。 :) :)

I'll flesh this out a little more in answer-form for you, though I'm not going to post any code because I don't think it's necessary to answer this particular question. 尽管我不会发布任何代码,因为我认为没有必要回答这个特定问题,但是我将以答案的形式为您充实。

Your database structure should look something like this (a lot of fields that a car rental company would want - such as mileage, damage, etc - are left out for brevity): 您的数据库结构应如下所示(为简洁起见,租车公司想要的许多字段(例如里程,损坏等)被省去了):

cars 汽车

  • car_id car_id
  • make 使
  • model 模型
  • year

customers 顾客

  • customer_id 顾客ID
  • first_name 名字
  • last_name
  • email_address 电子邮件地址

reservations 预约

  • reservation_id Reservation_id
  • car_id car_id
  • customer_id 顾客ID
  • start_date 开始日期
  • end_date 结束日期

The idea is that you never store a booking status with the car . 这个想法是,您永远不会用car存储预订状态。 Rather, you create a reservation with start_date and end_date set to the dates of the reservation. 而是,您创建一个reservation并将start_dateend_date设置为保留的日期。

Your query to check cars that are available for a given date range would be: 用于检查在给定日期范围内可用的汽车的查询将是:

SELECT
    c.car_id
FROM
    cars c 
    LEFT JOIN reservations r ON (
       c.car_id = r.car_id
       AND r.start_date <= $reservationEndDate
       AND r.end_date >= $reservationStartDate
    )
WHERE
    r.reservation_id IS NULL

And with this model, you never need to worry about running time-based jobs to update a cars reserved status. 使用此模型,您无需担心运行基于时间的作业来更新汽车保留状态。 Although, to optimize your application, you may run a daily job that updates the cars status so you can quickly find out how many cars are available today . 虽然,为了优化您的应用程序,您可以运行每日工作来更新汽车的状态,以便您可以快速找出今天有多少汽车可用。

Additional Note 附加说明

As NB pointed out in the comments - you'll want to make sure your application doesn't allow conflicting date ranges in the reservations table. 正如NB在评论中指出的那样-您将要确保您的应用程序不允许在保留表中有冲突的日期范围。 There's no unique index that can solve for this, so you'll need to handle this in your application logic - beware of race conditions. 没有唯一的索引可以解决此问题,因此您需要在应用程序逻辑中处理此问题-注意竞争条件。 A possible solution would be to create a temporary reservation as soon as someone looks at a car for a certain date range. 一种可能的解决方案是,只要有人在特定日期范围内看车,就立即创建一个临时预订。

Another Additional Note 另一个补充说明

NB Also had a good suggestion to avoid the above problem: use database triggers to validate it server-side. 注意:还有一个避免上述问题的好建议:使用数据库触发器在服务器端对其进行验证。 The database can reject the INSERT in the trigger if another booking was created that conflicts. 如果创建了另一个冲突的预订,数据库可以拒绝触发器中的INSERT。 This solution may very well be the best, as it keeps the validation as close to the data as possible (so you can't "forget" it in your application). 该解决方案可能是最好的,因为它使验证尽可能地接近数据(因此您不能在应用程序中“忘记”它)。

The literal answer to your question is cron (assuming you're running on a *nix system). 问题的字面答案是cron (假设您在* nix系统上运行)。

However, you really don't want to do that - it's very fragile, because you still have to query a table with bookings whenever you run the cron job; 但是,您确实不想这样做-这非常脆弱,因为每当运行cron作业时,您仍然必须查询带有预订的表; that means that now there are 3 places which know how car bookings work (the cron job, the bookings table, and your application). 这意味着现在有3个地方知道汽车预订的工作方式(cron工作,预订表和您的申请)。

It's far simpler to say that a car is unavailable to book by joining it to the bookings table; 简单地说,将汽车加入预订表就无法预订汽车。 if there's a valid booking for a given date, the car can be considered unavailable. 如果在给定日期有有效的预订,则可以认为该车不可用。

Making some assumptions about your schema, it might be something like 对您的架构进行一些假设,可能类似于

select 'booked'
from   cars c, 
       bookings b
where  c.car_id = b.car_id
and    b.start_date < ?required_date 
and    b.end_date   > ?required_date
union
select 'free'
from   cars c, 
       bookings b
where  c.car_id = b.car_id
and    b.start_date >= ?required_date 
or    b.end_date   <= ?required_date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM