如何检查提交的日期在数据库中的两个日期之间? [英] How Can I Check The Date Submitted is Between Two Dates in Database?

查看:89
本文介绍了如何检查提交的日期在数据库中的两个日期之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,其中有一个称为日期的表.此表中有start_date和end_date,两种类型都是date.举例来说,假设表格中的开始日期为2018年1月25日,结束日期为2018年1月31日.如何阻止其他人提交我的表单,并且两者之间的日期在此日期之间.

I have a database which has a table called dates. In this table there is start_date and end_date, both types are date. Let's say for example in the table there is a start_date of 2018-01-25 and an end_date of 2018-01-31. How can I stop someone else submitting my form with a date in between the two.

这是我的表格:

<form method="POST" action="dates.php" enctype="multipart/form-data" autocomplete="off">

    <label>Start Date *</label>
    <input name="startDate" id="startDate" type="date" required>
    <label>End Date *</label>
    <input name="endDate" id="endDate" type="date" required>

    <input type="submit" name="submit" value="Submit">
    <input type="reset" name="reset" value="Reset">

</form>

提交表单后,日期将以与我之前提到的格式相同的格式放置在表格中.

Once the form has been submitted the dates are placed in the table in the same format as I mentioned earlier.

在dates.php中,我像这样获得startDate和endDate:

In dates.php I get the startDate and endDate like so:

$start = $_REQUEST['startDate'];
$end = $_REQUEST['endDate'];

然后我尝试添加某种形式的验证:

I then try and add some form of validation:

    $sql = " SELECT * FROM dates WHERE start_date = '$start' AND end_date = '$end'";
    $result = mysqli_query($conn, $sql);
    $row_cnt = mysqli_num_rows($result);

        if($row_cnt > 0) {
            $isBooked = true;
        } else {
            $isBooked = false;
        }

然后我有了一个函数,如果isRepeat = true,它将重定向到错误页面,而不是提交重复的数据.

I then have a function so that if isRepeat = true it redirects to an error page instead of submitting duplicate data.

我觉得验证位是代码的一部分,但是我不确定该如何解决.

I have a feeling the validation bit is the part of code which is broken but I am not sure how to fix it.

推荐答案

在SQL查询中使用where子句.它将根据条件过滤选定的行.您可以在此处指定开始日期和结束日期.它的查询就是这样.

Use where clause in your sql query. It will filter the selected rows based on conditions. You can specify your start and end date there. The query for it, is something like that.

start_date = yourStartDate AND end_date = yourEndData中的AND表示如果两个条件都成立,则选择该行,否则不选择它.

the AND in the start_date = yourStartDate AND end_date = yourEndData mean if both conditions are true then select the row otherwise don't select it..'

SELECT * FROM dates WHERE start_date = yourStartDate AND  end_date = yourEndData

注意:您不再需要验证(if语句),只需检查查询是否返回了以下任何行.

Note: you dont need to validation (the if statement) anymore just check if the query returned any rows like below.

$isRepeat = (mysqli_num_rows($result)==0);

这篇关于如何检查提交的日期在数据库中的两个日期之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆