Avoid duplicate date and time for same scheduling professional

-1

Friends I'm trying to create a schedule for a dental clinic, but I'm having a hard time avoiding the client scheduling a date and time that has already been scheduled for that selected professional. For example, someone has already scheduled the service with the professional João on 06/01/2015 at 12: 00Hs, I would like someone else not to be able to schedule for the same professional the same date and time, that the system warned to choose another date and time for that professional or schedule for another professional available for the date and time you want.

I am using this code below that registers, but does not prevent duplicate registration of the schedule.

<?php if(isset($_POST['enter'])){

$nome = $_POST['nome'];
$tel = $_POST['tel'];
$cel = $_POST['cel'];
$email = $_POST['email'];
$plano = $_POST['plano'];
$horas = $_POST['horas'];
$prof = $_POST['prof'];
$data = $_POST['data'];
$tempo = date("dd/mm/YY His",time());


$pdo = new PDO('mysql:host=localhost;dbname=site', "root", "");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $pdo->prepare("SELECT * FROM agendar WHERE data LIKE = '{$data}' AND horas ='{$horas}' AND prof = '{$prof}'");
if($stmt->rowCount()>=1){

echo "<meta http-equiv='refresh' content='0; URL= agenda.php'>
      <script type=\"text/javascript\">
      alert(\"Esta data e hora já esta agendada para esse Profissional!<br />
              Tente com outro Profissional ou outra data e hora!<br />
              Obrigado!!!\");</script>";

 return die;

 }else{

 $stmt = $pdo->prepare ('INSERT INTO agendar (nome, tel, cel, email, plano, prof, data, horas)
                        VALUES (:nome, :tel, :cel, :email, :plano, :prof, :data, :horas)');

 $stmt->execute(array(':nome' => $nome,
                     ':tel' => $tel,
                     ':cel' => $cel,
                     ':email' => $email,
                     ':plano' => $plano,
                     ':prof' => $prof,
                     ':horas' => $horas,
                     ':data' => $data,
                     ':data' => $data,
                     ));

  if($stmt == ''){
    echo "<script language='javascript'>
          window.alert('Ocorreu um erro ao Agendar sua Avaliação!');
          </script>";
  }else{
    echo "<script language='javascript'>
          window.alert('Avaliação Agendada com sucesso!');
          </script>";

}}}
?>

I use this line of code $sql = mysql_query("SELECT * FROM agendar WHERE data LIKE '".$data."' AND horas ='".$horas."' AND prof = '".$prof."'"); if(mysql_num_rows($sql)>=1){ in Mysql to avoid duplicity, and it works cool, preventing the scheduling.

I adapted to the PDO thus getting $stmt = $pdo->prepare("SELECT * FROM agendar WHERE data LIKE = '{$data}' AND horas ='{$horas}' AND prof = '{$prof}'"); if($stmt->rowCount()>=1){ but I do not know if I did the adaptation correctly, and I think that may be where I'm wrong!

As requested I'm entering the FORM code below:

<form name="enter" method="post" action="" enctype="multipart/form-data">
<table cellpadding='2' cellspacing='2'>
<tr>
<td align="center"><span style="font:15px Verdana, Geneva, sans-serif; color:#000066;">AGENDE HOJE MESMO SUA AVALIAÇÃO</td>
</tr>
<tr>
<td><span>Nome:</span><input style="width:250px" type="text" name="nome" value=''></td>
</tr>
<tr>
<td><span>Telefone Residêncial:</span><input style="width:166px" type="text" name="tel" value='(21) '></td>
</tr>
<tr>
<td><span>Telefone Celular:</span><input style="width:188px" type="text" name="cel" value='(21) '></td>
</tr>
<tr>
<td><span>E-mail:</span><input style="width:247px" type="text" name="email" value=''></td>
</tr>
<tr>
<td align="center">
<?php 
$conn = new PDO('mysql:host=localhost;dbname=site', 'root', '');
$stmt = $conn->prepare('SELECT plano FROM planos');
$stmt-> execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
if(isset($_POST['enter'])){

$plano = $_POST['plano'];
$pdo = new PDO('mysql:host=localhost;dbname=site', "root", "");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $pdo->prepare('UPDATE agendar SET plano = :plano');
$stmt->execute(array( ':plano' => $plano));
}
?>
<select name="plano">
<?php foreach($result as $row){ ?>
    <option value="<?php echo $row['plano'];?>"><?php echo $row['plano'];?></option>
    <?php } ?>
</select><br />
<?php 
$conn = new PDO('mysql:host=localhost;dbname=site', 'root', '');
$stmt = $conn->prepare('SELECT prof FROM profissionais');
$stmt-> execute(); 
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
if(isset($_POST['enter'])){

$prof = $_POST['prof'];
$pdo = new PDO('mysql:host=localhost;dbname=site', "root", "");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $pdo->prepare('UPDATE agendar SET prof = :prof');
$stmt->execute(array( ':prof' => $prof));
}
?>
<select name="prof">
 <?php foreach($result as $row){ ?>
    <option value="<?php echo $row['prof'];?>"><?php echo $row['prof'];?></option>
    <?php } ?>
 </select><br />
</td>
</tr>                       
<tr>
 <td><input width="10" type="text" name="data" value="Data" id="data" />
<img src="img/calendar.png" width="30" height="30" style="float: none; margin-top: -22px; cursor: pointer;" title="Selecione a data do Agendamento!" onClick="$('#data').focus();" />
<?php 
$conn = new PDO('mysql:host=localhost;dbname=site', 'root', '');
$stmt = $conn->prepare('SELECT horas FROM horarios');
$stmt-> execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
if(isset($_POST['enter'])){

$horas = $_POST['horas'];
$pdo = new PDO('mysql:host=localhost;dbname=site', "root", "");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $pdo->prepare('UPDATE agendar SET horas = :horas');
$stmt->execute(array( ':horas' => $horas));
}
?>
<select name="horas">
<?php foreach($result as $row){ ?>
    <option value="<?php echo $row['horas'];?>"><?php echo $row['horas'];?></option>
    <?php } ?>
</select><br />
</td>
</tr>                       
<td><input class="input" type="submit" name="enter" value="Agendar" />  <span>Se preferir, ligue para: (21)2290.3702</span></td>
</tr>
</table>
</form>

If friends can help me, I'll be grateful.

Hugs to all !!!

    
asked by anonymous 30.05.2015 / 04:22

1 answer

0

I have analyzed your codes, and I think you could do it in stages so that you do not have a problem with duplicity ...

Before the user register name, address, etc ... he has to choose 1 day that it is better for him to make the appointment in the clinic. Then he chooses the times that are available on that date selected. And then it records the personal data ...

I formatted your code as well

agendar.php

      // aqui o codigo verifica se existe na url a data e a hora, se não existir ele executa baixo...
     <?php if(!isset($_GET['data']) && !isset($_GET['hora'])){ ?>

    <form method="get" action="agendar.php">
     <td><input width="10" type="text" name="data" value="Data" id="data" />
    <img src="img/calendar.png" width="30" height="30" style="float: none; margin-top: -22px; cursor: pointer;" title="Selecione a data do Agendamento!" onClick="$('#data').focus();" />
    <input width="10" type="submit" name="envia" value="Selecionar" /> 
     </form>
    // aqui o codigo verifica se existe na url a data e a hora, se não existir a hora ele executa baixo...
   <?php  } elseif(isset($_GET['data']) && !isset($_GET['hora']) { ?>

     <form method="get" action="agendar.php">

    <?php 

    //***** alterado ***** > a variável que estava escrita era a $pdo
    $conn= new PDO('mysql:host=localhost;dbname=site', "root", "");

    $data = $_GET['data'];
    //MUITO IMPORTATE: aqui estou selecionando apenas os horários vazios da tabela horário conforma a tabela agendar daquela data selecionada.
    $stmt = $conn->prepare("SELECT horas FROM horarios LEFT JOIN agendar ON agendar.horas = horarios.horas WHERE horarios.horas IS NULL AND agendar.data = $data");
    $stmt-> execute();
    //repare que eu alterei esta linha de código pois não estava funcionando com ASSOC, afinal oque você precisa é de um array
    $result = $stmt->fetchAll();
    if(isset($_POST['enter'])){

    $horas = $_POST['horas'];
    $pdo = new PDO('mysql:host=localhost;dbname=site', "root", "");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $pdo->prepare('UPDATE agendar SET horas = :horas');
    $stmt->execute(array( ':horas' => $horas));
    }
    ?>
    <select name="horas">
    <?php foreach($result as $row){ ?>
        <option value="<?php echo $row['horas'];?>"><?php echo $row['horas'];?></option>
        <?php } ?>
    </select><br />
     <input width="10" type="submit" name="envia" value="Selecionar" />
   </form>
         // aqui o codigo verifica se existe na url a data e a hora, se existir a hora e a data ele executa baixo..
        <?php  } elseif(isset($_GET['data']) && isset($_GET['hora']) { ?>

       <form name="enter" method="post" action="" enctype="multipart/form-data">
        <table cellpadding='2' cellspacing='2'>
        <tr>
        <td align="center"><span style="font:15px Verdana, Geneva, sans-serif; color:#000066;">AGENDE HOJE MESMO SUA AVALIAÇÃO</td>
        </tr>
        <tr>
         <td align="center"><span style="font:15px Verdana, Geneva, sans-serif; color:#000066;">Data selecionada: <input name="data" value="<?php echo $_GET['data'] ?>"/></td>
        </tr>
        <td align="center"><span style="font:15px Verdana, Geneva, sans-serif; color:#000066;">Hora selecionada:  <input name="hora" value="<?php echo $_GET['hora'] ?>"/></td>
        </tr>

        <tr>
        <td><span>Nome:</span><input style="width:250px" type="text" name="nome" value=''></td>
        </tr>
        <tr>
        <td><span>Telefone Residêncial:</span><input style="width:166px" type="text" name="tel" value='(21) '></td>
        </tr>
        <tr>
        <td><span>Telefone Celular:</span><input style="width:188px" type="text" name="cel" value='(21) '></td>
        </tr>
        <tr>
        <td><span>E-mail:</span><input style="width:247px" type="text" name="email" value=''></td>
        </tr>
        <tr>
        </table>
        </form>
        <?php  }  ?>

Now just insert into the database

I made the method get just so you have an idea.

But that selection of the time of day that is available will avoid duplicity for you, as it will only select the time fields from the time table that are available.

I hope I have helped.

    
30.05.2015 / 18:02