How do I make sure I can’t book the same Equipment for the same day and time?

Asked

Viewed 73 times

0

I’m making a code for 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 Adm, because it is he who makes the reservations, cannot reserve the same equipment twice for the same day and time. So I tried to do the following at the 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 book a piece of equipment that is already reserved in the bank the code simply ignores the second iflike he wasn’t even there. the if ignored is this

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 as instead of == put only one = but then when I do this all future reservations, even those not then in the bank will automatically enter the if

  • That doesn’t make any sense: if($sql == "SELECT * from reservas where id_equipamento= '.... - You are not testing the query result (you did not even run it).

  • So what would be the best solution for me to test and run the Query ?

  • mysql_query($sql,$con); and test if it has brought results. But this is not the right way. has to for a single input taking the desired columns, try to insert and check if duplicate key error has occurred.

  • the solution is here: https://answall.com/questions/23014/70

2 answers

1


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

In the 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 in your system this query does not seem to filter per user, that is, when you search if any user has already reserved the equipment for the day and time the system will reply saying "you have already reserved..."

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

E Just to remember, for future codes it would be better if you use MYSQLI and not MYSQL.

0

Your second "if" is just 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-execute the query that checks whether a reservation already exists. 2-check whether the return of the query is greater or equal to 1, as in this case you will already have a reservation (here is your second if).

Got the idea?

Browser other questions tagged

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