Save field type date problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mariolopes
Forum Contributor
Posts: 102
Joined: Sun May 22, 2005 7:08 am

Save field type date problem

Post 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");
}
?>
 
 
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Save field type date problem

Post 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')"; ?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mariolopes
Forum Contributor
Posts: 102
Joined: Sun May 22, 2005 7:08 am

Re: Save field type date problem

Post by mariolopes »

It works and I'll try to understand why
Thank you
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Save field type date problem

Post 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'.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mariolopes
Forum Contributor
Posts: 102
Joined: Sun May 22, 2005 7:08 am

Re: Save field type date problem

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Save field type date problem

Post 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].
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mariolopes
Forum Contributor
Posts: 102
Joined: Sun May 22, 2005 7:08 am

Re: Save field type date problem

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Save field type date problem

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
mariolopes
Forum Contributor
Posts: 102
Joined: Sun May 22, 2005 7:08 am

Re: Save field type date problem

Post 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);
 }
 
Post Reply