3
I’m changing the fields to save date in Mysql, fromvarchar
for date
, and I was in doubt about the correct way to include the date fields in the database using prepared statments
.
As DD/MM/YYYY use during the script to make some comparisons etc, I wanted to make the change only when including in the bank, during the insert
.
I don’t think you can use the STR_TO_DATE( string, formato )
with prepared statments
, as indicated in this answer, then, following this outra resposta
I adapted it like this:
if ($stmt = mysqli_prepare($mysqli, $sql)) {
mysqli_stmt_bind_param
(
$stmt,
's',
date("Y-m-d", strtotime($dataDMY)),
...
And it worked, but debugging I get a notice undefined variable
, that did not appear without putting date("Y-m-d", strtotime($dataDMY))
within the execution of bind::param
. This way is correct? What is the ideal way?
where do you come from
$dataDMY
?– Daniel Omine
From a form field. It comes in dd/mm/yyyy format...
– gustavox
but where is this variable set in php? There must be something like
$dataDMY = $_POST['dataDMY']
.. something there– Daniel Omine
Ah, so, it’s in another file, this file is just to include in the comic, and include with
include
at the end of the form output file... then when it runs it finds the variable, but debugging gives this notice that it did not give before...– gustavox
Do it like this, before this stretch
if ($stmt = mysqli_prepare($mysqli, $sql)) {
, put thisprint_r($_POST); print_r($_GET); exit;
And put the result in your question.– Daniel Omine
So the exit to this field with the
print_r
is for example:[dataDMY]=> 11/10/2010
... Tomorrow I edit the question, because now the sleep even hit :) thanks for now! Hugs!– gustavox
The idea is to receive the variable in the format
d/m/Y
and convert toY-m-d
and then record in the bank.– rray
That @rray, that’s right...
– gustavox
Good Q solved the problem, about the
undefined variable
the code that occurs this is not in the question. o bind_param seems to accept only references as argument.– rray