Date input into MySql

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
romanbills
Forum Newbie
Posts: 8
Joined: Thu May 11, 2006 2:00 pm

Date input into MySql

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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?
romanbills
Forum Newbie
Posts: 8
Joined: Thu May 11, 2006 2:00 pm

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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']))."')";
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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...
romanbills
Forum Newbie
Posts: 8
Joined: Thu May 11, 2006 2:00 pm

Post 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?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

since you're not escaping out of your double quotes, you can remove the dots (.) around your date.
romanbills
Forum Newbie
Posts: 8
Joined: Thu May 11, 2006 2:00 pm

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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]}')";
romanbills
Forum Newbie
Posts: 8
Joined: Thu May 11, 2006 2:00 pm

Post by romanbills »

I tried that now im getting a new error:


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


?
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Post 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]}')";
romanbills
Forum Newbie
Posts: 8
Joined: Thu May 11, 2006 2:00 pm

Post by romanbills »

THANK YOU

IT WORKED is correct.

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