检查日期是否在数据库中且大于

时间:2017-11-23 11:03:43

标签: php mysql sql

我的网站上有日期类型输入。我想检查所选日期是否在数据库中,并且还大于2017-01-01。我有类似的东西。怎么做?

if(ISSET($_POST['receipt_date'])) {
    $receipt_code=$_POST['receipt_date'];

    $checkdata="SELECT receipt_date FROM receipts WHERE receipt_code='$receipt_code' ";

    $query=mysql_query($checkdata);

    if(mysql_num_rows($query)>0) {
        echo "exist";
    } else {
        echo "ok";
    }
    exit();
}

1 个答案:

答案 0 :(得分:0)

检查此代码段:

<?php 
$link_to_db = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');

if(isset($_POST['receipt_date'])) 
{
    $receipt_date = $_POST['receipt_date'];
    $recept_datetime = new Datetime($receipt_date); # use \ before it if you using namespaces

    $check_data = "SELECT receipt_date FROM receipts WHERE receipt_date = '".$recept_datetime->format('Y-m-d H:i:s')."'";

    $query = mysqli_query($link_to_db, $checkdata); # use mysqli instead of mysql functions

    if(mysqli_num_rows($query) == 0) {
        echo "ok";
    }

    $check_datetime = new Datetime('2017-01-01 00:00:00');

    # work only on php > 5.2.2
    if ($receipt_datetime > $check_datetime) {
        echo "check OK";
    }
 }
 ?>

希望这有帮助!