Page 1 of 1

Save field type date problem

Posted: Tue Feb 02, 2010 4:34 pm
by mariolopes
Hi
I have problems when saving values type of date. I don't know why but the value is saved as 000-00-00 and i don't understand why. Please look at my code:

Code: Select all

 
<?php
$data='2009-11-11';
$mysql_id = mysql_connect('localhost', 'xxx', 'xxx');
mysql_select_db('xxx',$mysql_id);
//--
$query="INSERT INTO registo_horas (Aluno,Data,N_horas,Confirmado)
VALUES ('$_POST[aluno]',$data,'$_POST[horas]','N')";
$result=mysql_query($query);
if(!$result){
//echo(mysql_error());
header("Location:confirm.html");
}
else{
header("Location:registohoras.html");
}
?>
 
 

Re: Save field type date problem

Posted: Tue Feb 02, 2010 5:03 pm
by social_experiment
Try :

Code: Select all

<?php $query="INSERT INTO registo_horas (Aluno,Data,N_horas,Confirmado)
VALUES ('$_POST[aluno]', '".$data."','$_POST[horas]','N')"; ?>

Re: Save field type date problem

Posted: Wed Feb 03, 2010 7:39 am
by mariolopes
It works and I'll try to understand why
Thank you

Re: Save field type date problem

Posted: Wed Feb 03, 2010 9:21 am
by social_experiment
I think if place the value inside the query without quotation marks it reads the value you input just as $data instead of the variable '$data'.

Re: Save field type date problem

Posted: Wed Feb 03, 2010 10:14 am
by mariolopes
Sorry. I tough is works but I'm wrong. Please help me with the following code:

Code: Select all

 
<?php
$mysql_id = mysql_connect('localhost', 'xx', 'xxx');
mysql_select_db('mariolopes',$mysql_id);
//--
$query="INSERT INTO registo_horas (Aluno,Data,N_horas,Confirmado) VALUES ('$_POST[aluno]',$_POST[data],'$_POST[horas]','N')";
$result=mysql_query($query);
if(!$result){
header("Location:confirm.html");
}
else{
//header("Location:registohoras.html");
}
?>
 
I can't save the data value in my database

Re: Save field type date problem

Posted: Wed Feb 03, 2010 11:43 am
by social_experiment

Code: Select all

<?php $query="INSERT INTO registo_horas (Aluno,Data,N_horas,Confirmado) VALUES ('$_POST[aluno]','$_POST[data]','$_POST[horas]','N')";?>
You forgot the quotation marks around $_POST[data].

Re: Save field type date problem

Posted: Wed Feb 03, 2010 3:29 pm
by mariolopes
Hum...
It doesn't work
If i do in my code

Code: Select all

 
echo "$_POST[data]";
 
i get the value 02/23/2010
But this value is not saved in my database.

Re: Save field type date problem

Posted: Wed Feb 03, 2010 3:39 pm
by AbraCadaver
mariolopes wrote:Hum...
It doesn't work
If i do in my code

Code: Select all

 
echo "$_POST[data]";
 
i get the value 02/23/2010
But this value is not saved in my database.
That's because it needs to be 2010-02-23 yes? Look at the PHP strtotime() function and then the date() function. You'll need to use these together.

Re: Save field type date problem

Posted: Thu Feb 04, 2010 4:00 am
by mariolopes
Thank you
Here is a function to solve the problem:

Code: Select all

 
$datita=formatDate($_POST['data']);
function formatDate($dDate){
 $dNewDate = strtotime($dDate);
 return date('Y/m/d',$dNewDate);
 }