How do I make sure a person can’t book a room on the same day?

Asked

Viewed 669 times

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 reserve table, in which are the data.

cod_booking,name,email,n_telef, checkin, checkout,nr_adults, tipo_quarto,precoT

Here I have 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>";
    }
?>
  • You must do a check with a SELECT. Using as a criterion in Where the ID is Person, Room and Date of Entry and Exit.

  • Diego I commented , can see what I did wrong pf v?

  • still have error? put the CREATE of your table there that I help you

  • 2

    The question is unclear, do you want to restrict that a single person makes more than one reservation a day? or you want each room to have only one reservation?

  • Is it inconceivable that you would migrate your system e.g. to Postgresql? Postgresql has types that represent time intervals and exclude indices that prevent, at the database level, you enter two reservations with conflicting intervals.

1 answer

-1


whereas the camps checkin and checkout are the dates, you have some alternatives:

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

2) Perform a database search before insertion, using sql:

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

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

Problems

  • Two people may have the same name, ex: João da Silva
  • The same person can enter their name differently on each reservation, imagining, by their code, that because you are using an input screen where the person enters your name in an open way

A solution would be to request on the screen the typing of Cpf, without spaces and points. This way it is safer to do the index or search by Cpf associated to 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"

Whereas you have already done the validation so that the checkin date is less than or equal to the checkout

  • -1 by SQL Injection

  • Please @ctgPi, edit the answer avoiding SQL Injection instead of criticizing directly, as your negative does not help the colleague who is asking

Browser other questions tagged

You are not signed in. Login or sign up in order to post.