PDO execute returns true but does not insert

Asked

Viewed 102 times

1

I am trying to enter a record in the bank using this code:

  $sql = "INSERT INTO tb_ConteudoExtra
        (Titulo_ConteudoExtra, Texto, Id_TipoConteudoExtra, Data, Hora)
        VALUES
        ('".$nomePagina."', '".$html."','0','".$data."','".$hora."')";

  $sql = $this->db->prepare($sql);
  $execute = $sql->execute();

If I give a var_dump in the $execute it will show that the return was TRUE, but when I will look at the bank table, nothing was inserted.

The first thing I did, was to print the variable $sql and try to run straight into the bank to find out the error, only it had no error, successfully inserts.

INSERT INTO tb_ConteudoExtra
            (Titulo_ConteudoExtra, Texto, Id_TipoConteudoExtra, Data, Hora)
            VALUES
            ('teste dois', '<html>
<head>
    <title></title>
</head>
<body>
<p>teste de conteudo dentro do CKEDITOR</p>
</body>
</html>
','0','05/02/2019','15:28:36')

I tried to find a solution, but I couldn’t find anything that helped me.

@EDIT I tested it that way too and it’s still the same:

$sql = "INSERT INTO tb_ConteudoExtra(Titulo_ConteudoExtra, Texto, Id_TipoConteudoExtra, Data, Hora)
            VALUES
            (':nomepagina', ':html','0',':data',':hora')";
    $sql = $this->db->prepare($sql);
    $sql->bindValue(":nomepagina",$nomePagina);
    $sql->bindValue(":html",$html);
    $sql->bindValue(":data",$data);
    $sql->bindValue(":hora",$hora);
    $execute = $sql->execute();
  • Have you seen if the autocommit is on? playing the values directly in the query you throw away the benefit of prepare query.

  • @rray I tested using the bindValue too, but gave in the same

  • @rray I edited there to show how I did with bindValue

1 answer

1


Some considerations about your code:

A) If the field Data be the type date, the format should be 'yyyy-mm-dd', which in its case would be: '2019-02-05' and not '05/02/2019'.

B) Test the example below:

     $sql = "INSERT INTO tb_ConteudoExtra
            (Titulo_ConteudoExtra, Texto, Id_TipoConteudoExtra, Data, Hora)
            VALUES (?, ?, ?, ?, ?)";
      $prepare = $this->db->prepare($sql);
      $array_parametros = [$nomePagina, $html, 0, $data, $hora];
      $execute = $prepare->execute($array_parametros); 
      var_dump($sql);
      var_dump($prepare);
      var_dump($array_parametros);
      var_dump($execute);

Any problem, put here what values returned by var_dump.

I hope I’ve helped.

  • The date field is actually varchar. I’m doing after prepare now, edited the post with the new code using bindValue. Thanks!

  • @Otaviosouzarocha Did my answer solve the problem? Remember to pass the SQL string as a parameter to prepare it and also to execute it.

  • Not solved yet, keeps returning true and not inserting

  • Have you tried using the code "C" informed in my reply? Type, copied and pasted?

  • I tried now, but it came back to me like this: Warning: Pdostatement::execute() expects at Most 1 Parameter, 2 Given[...] ... And this time the execute returned false

  • I tried to remove $sql and leave only the parameters, which is how the execute documentation asks, but it still didn’t work (although it returned true)..... It looked like this: $execute = $prepare->execute($aray_parametros);

  • Take the example now.

  • Returned true, but nothing was inserted in the bank

  • Use the new code and put the return of each var_dump.

Show 4 more comments

Browser other questions tagged

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