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 !!!