-2

I have date type input on my site. I want to check if selected date is in the database and also is greater than for example 2017-01-01. I have something like this. How to do that?

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();
}
MMPL1
  • 533
  • 7
  • 21

1 Answers1

0

Check this snippet :

<?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";
    }
 }
 ?>

Hope this helps !