简体   繁体   English

防止双重预订 PHP/MYSQL

[英]Prevent Double Booking PHP/MYSQL

Hello I am having trouble with a appointment booking database.您好,我在使用预约数据库时遇到问题。 I have been stuck on this for 3 days and I cant figure out what to do.我已经坚持了 3 天,我不知道该怎么做。 I have appointments with various lengths that I retrieve from a form using PHP POST.我有各种长度的约会,我使用 PHP POST 从表单中检索这些约会。 I then try to update the database with the values I received.然后我尝试用我收到的值更新数据库。 I want to stop users from double booking appointments and booking appointments while another one is going on.我想阻止用户重复预订约会和预订约会,而另一个约会正在进行。

This is my DB MYSQL DB .这是我的数据库MYSQL 数据库

The lesson_date_time field is in DATETIME format with a max value of 6.课程日期时间字段采用 DATETIME 格式,最大值为 6。

The form code表格代码

<form method="post" name="update" action="dtupp.php" /> 
    Email:<br>
    <input type="text" name="email" /><br>
    Lesson Length:<br>   
    <input type="text" name="length" /><br> 
    Start Date<br> 
    <input type="text" name="date"/><br>
    <input type="submit" name="Submit" value="update" /> 
</form> 

The values I pass into the form are我传递到表单中的值是

For Start Date: 2014-11-30 12:41:00开始日期:2014-11-30 12:41:00

For Lesson Length: 60课程长度:60

For Email: Fake@aol.com对于电子邮件:Fake@aol.com

My processor statement我的处理器声明

<?php 
$mins = $_POST["length"];
$date_start = $_POST["date"];
$date_end   = date("Y-m-d H:i:s", strtotime("$date_start + $mins minute"));
$emails = $_POST['emails']; 
mysql_connect("localhost", "dk1", "root1") or die("Connection Failed"); 
mysql_select_db("usersignup")or die("Connection Failed"); 
$query = " UPDATE bookings SET email = '$emails' WHERE lesson_date_time BETWEEN CAST('$date_start' AS DATETIME) AND CAST('$date_end' AS DATETIME) ";
if(!mysql_query($query)){
    die('Could not update data: ' . mysql_error());
}
else{
}
?>

For some reason I cannot get the values to into the database.出于某种原因,我无法将值放入数据库。 MYSQL doesn't return ANY ERRORS. MYSQL 不返回任何错误。 I checked if the error reporting is working by misplacing quotes and MYSQL immediately flags me.我通过错位引号检查错误报告是否有效,MYSQL 立即标记我。 It wont return any errors but wont post anything into the database either.它不会返回任何错误,但也不会将任何内容发布到数据库中。

The key thing you're missing here is having a condition that prevents over-writing previous bookings.您在这里缺少的关键是有一个条件可以防止覆盖以前的预订。 In the current state of things that will take the form:在目前的情况下,将采取以下形式:

UPDATE bookings SET ... WHERE email IS NULL

This presumes email is initially NULL which it should be.这假定email最初应该是NULL Avoid setting it to obtuse things like 'none' or an empty string.避免将其设置为诸如'none'或空字符串之类'none'钝化内容。

Note, you'll want to avoid violating the Zero, One or Infinity Rule and adhere to proper database normalization rules: Create a table that represents the individual making the booking.请注意,您需要避免违反零、一或无穷大规则并遵守适当的数据库规范化规则:创建一个代表进行预订的个人的表。 Even if this table only contains the email address at first, it's a huge step towards having proper database integrity.即使这个表最初只包含电子邮件地址,这也是朝着拥有适当的数据库完整性迈出的一大步。

Then you'd insert user_id instead, where that refers to a record in your users table.然后你会插入user_id ,它指的是你的users表中的一条记录。 This way if a user changes their email address you only have to update one row, not N rows in who knows how many tables.这样,如果用户更改了他们的email地址,您只需更新一行,而不是知道有多少个表的 N 行。

This establishes a proper "one-to-many" relationship between users and bookings.这在用户和预订之间建立了适当的“一对多”关系。 From there you can build out a very robust application.从那里你可以构建一个非常健壮的应用程序。

Maybe there a no rows for your WHERE statement.也许您的 WHERE 语句没有行。 For debugging check with SELECT if your WHERE statement returns anything.如果您的 WHERE 语句返回任何内容,请使用 SELECT 进行调试检查。

You also could check the affected rows of your statement with mysql_affected_rows() .您还可以使用mysql_affected_rows()检查语句中受影响的行。

I also wanted to mention that you should be aware of sql injections!我还想提一下,您应该了解 sql 注入!

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

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