A Question

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
Tralalah
Forum Newbie
Posts: 15
Joined: Tue Mar 09, 2010 8:50 am

A Question

Post by Tralalah »

Sirs, how do you save a time data to the database?

Or specifically, how do I save the time data to a mySQL database the moment I press a button?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: A Question

Post by Christopher »

Usually you would create a DATETIME field and set it to NOW() in your INSERT or UPDATE.
(#10850)
Tralalah
Forum Newbie
Posts: 15
Joined: Tue Mar 09, 2010 8:50 am

Re: A Question

Post by Tralalah »

How do you do that exactly, coding the button? I am at a loss how to use Post while using the Now() fcn.
wanger220
Forum Newbie
Posts: 19
Joined: Tue Feb 02, 2010 8:44 pm

Re: A Question

Post by wanger220 »

In whatever script that updates or inserts your record, include an additional field that sets the current date using the now() function.

$sql = "INSERT INTO table (field1, field2, ..., current_date) VALUES ('value1', 'value2', ..., 'now()')";



If you need to save the date/time in a particular format, you can also use date(), but how the timestamp is displayed can always be changed when you output the data.
Tralalah
Forum Newbie
Posts: 15
Joined: Tue Mar 09, 2010 8:50 am

Re: A Question

Post by Tralalah »

Wanger, thanks for that. But it does not seem to be posting the values of the now() / date() to the date field that I have.
My date field is in DATETIME.

Code: Select all

$sql="INSERT INTO Printing (name, page, date)
VALUES
('$_POST[name]','$_POST[page]', 'date()' )";
 
EDIT| I even did it this way:

Code: Select all

$deight=date("Y-m-d H:i:s");
$sql="INSERT INTO Printing (name, page, date)
VALUES
('$_POST[name]','$_POST[page]',$deight)";
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: A Question

Post by mikosiko »

Code: Select all

$sql="INSERT INTO Printing (name, page, date)
VALUES
('$_POST[name]','$_POST[page]', 'date()' )";
post the code that you are using to execute the $sql sentence ....
Tralalah
Forum Newbie
Posts: 15
Joined: Tue Mar 09, 2010 8:50 am

Re: A Question

Post by Tralalah »

mikosiko wrote:

Code: Select all

$sql="INSERT INTO Printing (name, page, date)
VALUES
('$_POST[name]','$_POST[page]', 'date()' )";
post the code that you are using to execute the $sql sentence ....
Here it is:

Code: Select all

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
EDIT| It's working now. Foolish of me to forget precious ' '. :D
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: A Question

Post by Luke »

Although you were able to get help with your question, from now on please use a more descriptive thread title when posting questions in the forums. I personally will not click on threads which have such vague titles except to tell the poster to make their title more specific. When people are browsing the forums, all they have to go by is your thread title, so make it easy on them to help you and give them a descriptive title. Thanks!
Tralalah
Forum Newbie
Posts: 15
Joined: Tue Mar 09, 2010 8:50 am

Re: A Question

Post by Tralalah »

Duly noted, Luke. Thanks for that. :)
Post Reply