Problem with INSERT using PDO

Asked

Viewed 246 times

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

inserir a descrição da imagem aqui

on line 143 inserir a descrição da imagem aqui

and on line 164

inserir a descrição da imagem aqui

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 using PDO we can’t count the columns with mysql_num_rows

2 answers

3


In the update pass date in format Y-m-d, can also pass current date directly by the bank using the function now() mysql.

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

To get the number of lines do not use mysql_num_rows because no connection to the mysql_* functions was created so it receives the error:

mysql_num_rows() expects Parameter 1 to be Resource, null Given in

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

Change:

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

For:

if($stmt->rowCount() >= 1){

0

Perfect friend, that was right now worked right, I’m entering 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>";

}}}}
?>

I hug you all, and you fight for help.

  • Murilo, if a reply rray helped you, mark it as accepted by clicking on the mark below the score.

  • @qmechanik, how you made the sign of 'right' ?

  • @rray Pego from here.

  • 1

    @qmechanik, thanks hehe the symbol is much better than the V.

  • Hello we solved the issue of INSERT, but I need to prevent the same hoario and date is scheduled with the professional who is already scheduled. I am using this code in MYSQL $stmt = $pdo->query("SELECT * FROM agendar WHERE data LIKE '".$data."' AND horas ='".$horas."' AND prof = '".$prof."'");, but this is not advancing, because it is repeating the scheduling data. Does anyone have any idea how I can prevent date duplicity, time for the same professional? Quarrel, and hug to all!!!

  • Better open another question @Murilocabral, not used like to search for dates.

Show 1 more comment

Browser other questions tagged

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