Page 1 of 1

Cannot save variable type of date

Posted: Fri Jan 22, 2010 5:49 pm
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

Re: Cannot save variable type of date

Posted: Fri Jan 22, 2010 6:12 pm
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.

Re: Cannot save variable type of date

Posted: Fri Jan 22, 2010 6:20 pm
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

Re: Cannot save variable type of date

Posted: Fri Jan 22, 2010 6:50 pm
by AbraCadaver

Re: Cannot save variable type of date

Posted: Fri Jan 22, 2010 7:20 pm
by mariolopes
Thank you