Page 1 of 1

Date input into MySql

Posted: Thu May 25, 2006 11:56 am
by romanbills
For some reason MySQL doesn’t like me today. I'm convinced it’s moody. So here is what's going on:

I have a form that submits its information into my MySQL database. I’m looking to add the date to this submission to the database. Unfortunately everything I try doesn’t return errors it just doesn’t input the date into my database table...

Any help would be appreciated.

Thanks.

-Roman

Posted: Thu May 25, 2006 1:16 pm
by Burrito
what type of field is the MySQL field (date, datetime, int, varchar, etc)?

what are you doing to try and insert the date currently?

Posted: Thu May 25, 2006 1:22 pm
by romanbills
Type i just set to VarChar and im using

$time = date('D M j');

Iv tried all different ways to input it but for some reason it just wont do it. I'v used the time function in the form and then used POST that didnt work then i just used it in my "enter_it" script and attempted to use it as a var in that. Still didnt work. Im out of ideas.

Posted: Thu May 25, 2006 1:28 pm
by Burrito
for starters, you should always use a date, datetime, or int (with unix timestamp) field for dates.

This is the only way that you'll be able to sort the date properly if you ever need to.

try something like this:

Code: Select all

$query = "INSERT INTO `myTable` (`date`) VALUES ('".date("Y-m-d"),strtotime($_POST['datefield']))."')";

Posted: Thu May 25, 2006 1:29 pm
by timvw
romanbills wrote:Type i just set to VarChar and im using
Mysql has more suited types for date and times.. Checkout the manual... (DATE seems most appropriate for you)

Code: Select all

INSERT INTO column (datecolumn) VALUES (NOW())
If you don't understand what the NOW() function returns, you'll know after you've read the chapter on date and time functions in the same manual...

Posted: Thu May 25, 2006 1:42 pm
by romanbills
Thanks for the tip...

I just tried to use the previous suggestion and it now gives me an error

Code: Select all

$query="INSERT INTO registration(date,first_name,MI,last_name,title,day_phone,day_location,evening_phone,evening_location,mobile_phone,preffered_phone,fax,email) VALUES ('.date("Y-m-d"),strtotime($_POST['datefield'])).','$_POST[first_name]','$_POST[MI]','$_POST[last_name]','$_POST[title]','$_POST[day_phone]','$_POST[day_location]','$_POST[evening_phone]','$_POST[evening_location]','$_POST[mobile_phone]','$_POST[preffered_phone]','$_POST[fax]','$_POST[email]')";
I get an un expected T_STRING error?

Posted: Thu May 25, 2006 1:49 pm
by Burrito
since you're not escaping out of your double quotes, you can remove the dots (.) around your date.

Posted: Thu May 25, 2006 1:56 pm
by romanbills
So heres the new code that I just ran :

Code: Select all

$query="INSERT INTO registration(date,first_name,MI,last_name,title,day_phone,day_location,evening_phone,evening_location,mobile_phone,preffered_phone,fax,email) VALUES ('date("Y-m-d"),strtotime($_POST['datefield'])','$_POST[first_name]','$_POST[MI]','$_POST[last_name]','$_POST[title]','$_POST[day_phone]','$_POST[day_location]','$_POST[evening_phone]','$_POST[evening_location]','$_POST[mobile_phone]','$_POST[preffered_phone]','$_POST[fax]','$_POST[email]')";
Its still returning the unknown T_STRING error. I cant identify anything wrong with it. This thing is haunting me.

Posted: Thu May 25, 2006 1:59 pm
by Burrito
try this:

Code: Select all

$query="INSERT INTO registration(date,first_name,MI,last_name,title,day_phone,day_location,evening_phone,evening_location,mobile_phone,preffered_phone,fax,email) VALUES ('".date("Y-m-d"),strtotime($_POST['datefield'])."','{$_POST[first_name]}','{$_POST[MI]}','{$_POST[last_name]}','{$_POST[title]}','{$_POST[day_phone]}','{$_POST[day_location]}','{$_POST[evening_phone]}','{$_POST[evening_location]}','{$_POST[mobile_phone]}','{$_POST[preffered_phone]}','{$_POST[fax]}','{$_POST[email]}')";

Posted: Fri May 26, 2006 10:42 am
by romanbills
I tried that now im getting a new error:


Parse error: parse error, unexpected ',' in enter_it.php on line 44


?

Posted: Fri May 26, 2006 10:58 am
by xpgeek
It worked!

Code: Select all

$query="INSERT INTO registration(date,first_name,MI,last_name,title,day_phone,day_location,evening_phone,
evening_location,mobile_phone,preffered_phone,fax,email)
 VALUES ('".date("Y-m-d")."','{$_POST[first_name]}','{$_POST[MI]}','{$_POST[last_name]}',
'{$_POST[title]}','{$_POST[day_phone]}','{$_POST[day_location]}','{$_POST[evening_phone]}',
'{$_POST[evening_location]}','{$_POST[mobile_phone]}','{$_POST[preffered_phone]}',
'{$_POST[fax]}','{$_POST[email]}')";

Posted: Fri May 26, 2006 11:10 am
by romanbills
THANK YOU

IT WORKED is correct.

I appreciate all the help. Thanks to anyone whom posted.