How can I not reserve the same Equipment for the same day and time?

-1

I am doing a code of Equipment Rental in Php, in general the code is almost all finished, but in the last step I came across a big problem, I wish the adm, since it is he who makes the reservations, not you can reserve the same equipment twice for the same day and for the same time. So I tried to do the following in Event

include_once'conexao.php';
 if(isset($_POST['nomeprof']))$atrNomeprof = $_POST['nomeprof'];
 if(isset($_POST['loginprof']))$atrLoginprof = $_POST['loginprof'];
 if(isset($_POST['senhaprof']))$atrSenhaProf = $_POST['senhaprof'];
 if(isset($_POST['telefoneprof']))$atrTelprof = $_POST['telefoneprof'];
 if(isset($_POST['cpfprof']))$atrCPFprof = $_POST['cpfprof'];
 if(isset($_POST['disciplinaprof']))$atrDiscprof = $_POST['disciplinaprof'];
 if(isset($_POST['emailprof']))$atrEmailprof = $_POST['emailprof'];
 if(isset($_POST['nomeequip']))$atrNomeequip = $_POST['nomeequip'];
 if(isset($_POST['corequip']))$atrCorequip = $_POST['corequip'];
 if(isset($_POST['marcaequip']))$atrMarcaequip = $_POST['marcaequip'];
 if(isset($_POST['categoriaequip']))$atrCategoriaequip = $_POST['categoriaequip'];
 if(isset($_POST['id_equipamento']))$atride = $_POST['id_equipamento'];
 if(isset($_POST['id_professor']))$atridp = $_POST['id_professor'];
if(isset($_POST['data']))$atrdata = $_POST['data'];
if(isset($_POST['hora']))$hora = $_POST['hora'];
if(isset($_POST['hora2']))$hora2 = $_POST['hora2'];
if(isset($_POST['sala']))$sala = $_POST['sala'];


if(isset($_GET['reserva'])){

if($sql == "SELECT * from reservas where id_equipamento= '" . $atride . "' AND horario='".$hora."' AND dt_reserva='".$atrdata."' "){
    echo $sql;
    echo "<script> alert('Você ja Reservou esse equipamento para esse Dia e Horario') </script>";
  }
  else{

 $sql = sprintf("INSERT INTO 'reservas' SET 'id_equipamento' = \"%d\", 'id_adm' = \"%d\",  'horario' = \"%s\", 'dt_reserva' = \"%s\", 'id_professor' = \"%d\";",
$atride,
 $userId,
 $hora,
 $atrdata,
 $atridp);
 // echo $sql;
 mysql_query($sql,$con);

 echo"<script> alert('Equipamento Reservado com sucesso')</script>";
 }
}

But when I try to reserve equipment that is already reserved in the bank, the code simply ignores the second if as if it were not even there. % ignored% is

if($sql == "SELECT * from reservas where id_equipamento= '" . $atride . "' AND horario='".$hora."' AND dt_reserva='".$atrdata."' "){
echo $sql;
echo "<script> alert('Você ja Reservou esse equipamento para esse Dia e Horario') </script>";}

I have analyzed this If several times and tried to make changes such as instead of if put only == but then when I do this all future reservations, even those that are not then in the database will automatically enter =

    
asked by anonymous 30.05.2018 / 02:16

2 answers

0

You can use mysql_num_rows ($ query) to see if there is already a result for your query.

In case:

$q="SELECT * FROM reservas WHERE id_equipamento= '" . $atride . "' AND horario='".$hora."' AND dt_reserva='".$atrdata."' ";
if(mysql_num_rows($q)){
    //Já existe na tabela.
    echo "<script> alert('Você ja Reservou esse equipamento para esse Dia e Horario') </script>";
}

If you have multiple users on your system this query does not seem to filter per user, ie when you search if any user has already booked the device for the day and time the system will respond saying "you already booked ..."

If you have multiple users you will need to filter this as well not to give future problems.

And just to remember, for future codes you'd better use MYSQLI instead of MYSQL.

    
30.05.2018 / 15:49
0

Your second "if" is only comparing the contents of the variable $sql with some string, in this case "SELECT * from reservas where id_equipamento= '" . $atride . "' AND horario='".$hora."' AND dt_reserva='".$atrdata."' " .

What you need to do is:

1-Run the query that checks whether a buffer already exists. 2-check if the query return is greater than or equal to 1, because in this case you will already have a reservation (here is your second if).

Got the idea?

    
30.05.2018 / 15:57