Problem with INSERT using PDO

1

I have this code below that is working perfectly, but I would like to change it to work in PDO but I am having trouble registering with DB when I change it to the PDO.

It refers to errors

online143

and line 164

ThecodeinMysqlthatisworkingisthisbelow:

<?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 looks 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 question is whether I'm making the change to PDO correctly, and would like the help of friends to find out where I'm going wrong. Thank you in advance for the attention of friends. Hugs to all!

    
asked by anonymous 28.05.2015 / 21:50

2 answers

3

Update pass the date in Y-m-d format, you can also pass current date directly from the database using the now () of mysql.

$tempo = date('Y-m-d H:i:s')

To get the number of rows do not use mysql_num_rows since no connection to the mysql_ * functions was created so you get the error:

  

mysql_num_rows () expects parameter 1 to be resource, null given in

The function / method equivalent to it in the PDO is [rowCount()][2] of class PDOStatement , which is the $stmt variable of your code.

Change:

if(mysql_num_rows($sql) >= 1){

By:

if($stmt->rowCount() >= 1){
    
28.05.2015 / 21:58
0

Perfect friend, that's right now it worked, I'm inserting the code with the changes.

<?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($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!                               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, 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>";

}}}}
?>

Hugs to all, and thanks for the help.

    
28.05.2015 / 23:57