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
mariolopes
Forum Contributor
Posts: 102 Joined: Sun May 22, 2005 7:08 am
Post
by mariolopes » Tue Feb 02, 2010 4:34 pm
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");
}
?>
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Tue Feb 02, 2010 5:03 pm
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
Post
by mariolopes » Wed Feb 03, 2010 7:39 am
It works and I'll try to understand why
Thank you
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Wed Feb 03, 2010 9:21 am
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
Post
by mariolopes » Wed Feb 03, 2010 10:14 am
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
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Wed Feb 03, 2010 11:43 am
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
Post
by mariolopes » Wed Feb 03, 2010 3:29 pm
Hum...
It doesn't work
If i do in my code
i get the value 02/23/2010
But this value is not saved in my database.
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Wed Feb 03, 2010 3:39 pm
mariolopes wrote: Hum...
It doesn't work
If i do in my code
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
Post
by mariolopes » Thu Feb 04, 2010 4:00 am
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);
}