1
I have this code below that is working perfect, but I would like to change it to work in PDO but I’m having problem in the registration with BD, when I change to PDO.
He refers to mistakes
on line 143
and on line 164
The Mysql code that is working is the one below:
<? 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());
$sql = mysql_query("SELECT * FROM agendar WHERE data LIKE '".$data."' AND horas ='".$horas."' AND prof = '".$prof."'");
if(mysql_num_rows($sql)>=1){
echo "<meta http-equiv='refresh' content='0; URL= http://www.rfclinica.com.br/index.php'>
<script type=\"text/javascript\">
alert(\"Esta data e hora já esta agendada para esse Profissional! Tente com outro Profissional ou outra data e hora! Obrigado!!!\");</script>";
return die;
}else{
$inserir = mysql_query("INSERT INTO agendar (nome, tel, cel, email, plano, prof, data, horas) VALUES ('$nome', '$tel', '$cel', '$email', '$plano', '$prof', '$data', '$horas')");
if($inserir == ''){
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>";
}}}?>
And the modification to work with PDO was like this:
<?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);
if( $pdo->query("SELECT count(*) FROM agendar WHERE prof = '{$prof}'")->fetchColumn() <=0) {
$stmt = $pdo->prepare("SELECT * FROM agendar WHERE data LIKE '".$data."' AND horas ='".$horas."' AND prof = '".$prof."'");
if(mysql_num_rows($sql)>=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! Tente com outro Profissional ou outra data e hora! Obrigado!!!\");</script>";
return die;
}else{
$stmt = $pdo->prepare ('INSERT INTO agendar (nome, tel, cel, email, plano, prof, data, tempo)
VALUES (:nome, :tel, :cel, :email, :plano, :prof, :data, :tempo)');
$stmt->execute(array(':nome' => $nome,
':tel' => $tel,
':cel' => $cel,
':email' => $email,
':plano' => $plano,
':prof' => $prof,
':data' => $data,
':tempo' => date("dd/mm/YY His",time())
));
if($inserir == ''){
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>";
}}}}
?>
My doubt is whether I am making the change to PDO correctly, and I would like the help of friends to find out where I am going wrong. Thank you for your friends' attention. Hug to all!
Look at that stretch:
if(mysql_num_rows($sql)>=1){
The $sql variable is not set at the time the code is executed. When usingPDO
we can’t count the columns withmysql_num_rows
– gmsantos