Cannot save variable type of date

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

Cannot save variable type of date

Post by mariolopes »

Hi
I need to save one variable type date in mysql table but i can't. Please look at the code and help me, please:

$data_compra=date($_POST['data_compray']."/".$_POST['data_compram']."/".$_POST['data_comprad']);
print $data_compra;
//acesso à base de dados
$mysql_id = mysql_connect('localhost', "curso", "123");
mysql_select_db('mariolopes',$mysql_id);
$query="insert into negocios (Codigo,Data_compra)values('Tobias',$data_compra)";
$result=mysql_query($query);

$data_compra is my variable type of date. If I can have date in european format is better (dd/mm/yy) for my project.
Thank you
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Cannot save variable type of date

Post by requinix »

mariolopes wrote:$data_compra is my variable type of date.
There is no "variable type of date". PHP doesn't have it.

What you have is a string, and in MySQL strings need quotes around them.

Code: Select all

$query="insert into negocios (Codigo,Data_compra)values('Tobias','$data_compra')";
Also, your $data_compra assignment is wrong. You shouldn't be using date() for it.
Also, you don't validate the date. Someone could mess with your form and screw up your script. Use checkdate.
mariolopes
Forum Contributor
Posts: 102
Joined: Sun May 22, 2005 7:08 am

Re: Cannot save variable type of date

Post by mariolopes »

Thank you
Problem solved. My other question: how can I retrieve the data from my table in the format dd-mm-YYYY
Thank you
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Cannot save variable type of date

Post by AbraCadaver »

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: Cannot save variable type of date

Post by mariolopes »

Thank you
Post Reply