-1
I am trying to enter a register in the database as follows (PHP)
$sql = "INSERT INTO `tb_teste` (`estrutura`, `foto`, `data_cadastro`) VALUES (':estrutura', ':foto', ':data_cadastro')";
$sql = $this->db->prepare($sql);
$sql->bindParam(":estrutura", $estrutura, PDO::PARAM_STR);
$sql->bindParam(":foto", $foto, PDO::PARAM_STR);
$sql->bindParam(":data_cadastro", $data_cadastro, PDO::PARAM_STR);
$retorno = $sql->execute();
I’ve tried with bindValue
, I have tried without using the last parameter PDO::PARAM_STR
, already tried to convert to Timestamp, converted before sending to PHP (in jQuery), converted several different ways in PHP and nothing worked, I keep getting the error:
Incorrect datetime value: ':data_cadastro' for column 'data_cadastro' at Row 1
The only thing I did and inserted into the bank was this:
$sql = "INSERT INTO `tb_teste` (`estrutura`, `foto`, `data_cadastro`) VALUES (':estrutura', ':foto', '$data_cadastro')";
$sql = $this->db->prepare($sql);
$sql->bindParam(":estrutura", $estrutura, PDO::PARAM_STR);
$sql->bindParam(":foto", $foto, PDO::PARAM_STR);
putting the date variable in the query did not give error and entered the date in the field correctly, but the other fields were with the value :estutura
,:foto
NOTE: value displayed in var_dump($data_registration):
C: wamp64 www TEST controllers panel testeController.php:57:string '1995-11-14 00:00:00'
That is, when I try to use bindParam or bindValue the insertion does not work, but if the value is inserted in the query, it works correctly.
Thank you!
can display the variable value
data_cadastro
? seems to me to be a date format problem– Ricardo Pontual
@Ricardopunctual The value of the variable is '1995-11-14 00:00:00', updated there in the post also
– Otavio Souza Rocha
vc converts the format date
dd/mm/YYYY
forYYYY-mm-dd
? looks like there’s an error in the conversion.– rray
@rray I was able to insert the value of the $data_cadastre variable into the database, but only by placing the variable directly in the sql query, as in the second example... the error only occurs when it passes through the bindParam
– Otavio Souza Rocha
humm is not missing prepare this query? something like
$sql = $mysqli->prepare("INSERT INTO .....
?– Ricardo Pontual
@Ricardopunctual I’m already doing it, I’ll update there including
– Otavio Souza Rocha
In the first example there are simple quotes left,
':data_cadastro')
, no quotation marks on the place.– rray
Why the ":"?
$sql->bindParam("estrutura", $estrutura, PDO::PARAM_STR);
$sql->bindParam("foto", $foto, PDO::PARAM_STR);
– Maury Developer
@Maurydeveloper followed the PHP documentation https://www.php.net/manual/en/pdostatement.bindparam.php
– Otavio Souza Rocha
Actually I said the quotes are useless,
:
.– Maury Developer