How do I prevent a person from reserving the room on the same day?

2

I have the reservation code made, but the SELECT is so that people can not book the room on the same day I can not do. In my database I have the reservation table, in which the data is found.

  

backup_name, name, email, n_telef, checkin, checkout, nr_adults, room_type, precoT

Here is the code:

 <?php
    $link = mysqli_connect("localhost","root","","hotel");

    $nome=$_POST['Nome'];
    $email=$_POST['email'];
    $telefone=$_POST['telefone'];
    $checkin=$_POST['checkin'];
    $checkout=$_POST['checkout'];
    $nr_adultos=$_POST['nr_adultos'];
    //$n_noites=$_POST['n_noites'];

    $tipo_quarto=$_POST['tipo_quarto'];
    $precoT=$_POST['pre']; 

        // verificacao de dados e feito em javascript e/ou html php so para comunicacao com servidor;

    $query = "INSERT INTO reserva VALUES (NULL, '$nome', '$email', $telefone, '$checkin', '$checkout', $nr_adultos,'$tipo_quarto',$precoT)";
    $result = mysqli_query ($link ,  $query);

    if($result){
        echo "<script> myFunction('Reserva feita'); </script>";
    }else{
        echo "<script> myFunction('Erro ao fazer a reserva'); </script>";
    }
?>
    
asked by anonymous 01.06.2015 / 21:19

1 answer

-1

Since the fields checkin and checkout are the dates, you have some alternatives:

1) Define a unique index in the database with the fields: nome, checkin , so the person does not make two reservations on the same day.

2) Do a search on the database before insertion, using sql:

 select * from reserva where nome = '" . $nome . "' and checkin = '" . $$checkin . "'

If the search finds a result, it does not make the reservation.

Problems

  • Two people can have the same name, eg: João da Silva
  • The same person can enter his name differently on each reservation, imagining, by his code, that because you are using an input screen where the person type his name openly

One solution would be to request on-screen typing of cpf, without spaces and periods. This makes it safer to index or search by cpf associated with the date.

Other consideration, if you intend to test the checkin and checkout interval, use the search with the following comparisons after the WHERE clause:

$checkin . "' >= checkin AND " . $checkin . "' <= checkout"

Considering that you have already validated so that the checkin date is less than or equal to the checkout

    
16.06.2015 / 20:39