For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > April 2004 > Re: [PHP-DB] Inserting date into a table









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PHP-DB] Inserting date into a table
Rachel Rodriguez

2004-04-24, 2:30 am


> I want to insert into the TIMESTAMP field the date
> automatically. How can I
> do it using the insert command
>
> "INSERT INTO $table
>

VALUES('','$name','TIMESTAMP','$question
','$email','NULL')";
>


Use the word null (no quotes) in place of 'TIMESTAMP':

INSERT INTO $table
VALUES('', '$name', null, '$question', '$email',
null)";

The above will work in MySQL. I haven't tried it in
other databases.

~R




__________________________________
Do you Yahoo!?
Yahoo! Photos: High-quality 4x6 digital prints for 25¢
http://photos.yahoo.com/ph/print_splash
John W. Holmes

2004-04-24, 9:32 am

Pambos Nicolaou wrote:

> I have created the table below:
>
> CREATE TABLE questions (ID INT NOT NULL AUTO_INCREMENT,name
> VARCHAR(30),day TIMESTAMP, question TEXT, email VARCHAR(30),answer TEXT,
> PRIMARY KEY(ID));
>
> I want to insert into the TIMESTAMP field the date automatically. How
> can I do it using the insert command
>
> "INSERT INTO $table
> VALUES('','$name','TIMESTAMP','$question
','$email','NULL')";


Two ways:

1: INSERT INTO $table (name, question, email) VALUES
('$name','$question','$email')

This way, the ID and TIMESTAMP columns will be populated automatically.
The ID column will get the next available number and the "day" column
will be assigned the current date/time. Note how you can leave out the
"answer" column, too, since you weren't assigning a value to it, anyhow.
It will be given the default value of the column, which in this case is
NULL.

2: INSERT INTO $table (name, day, question, email) VALUES
('$name',NULL,'$question','$email')

Setting the TIMESTAMP column to NULL will cause it to be set to the
current date/time. This works for the first TIMESTAMP column in a table
(since you only have one, it doesn't matter).

I recommend method 1.

--
---John Holmes...

Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/

php|architect: The Magazine for PHP Professionals – www.phparch.com


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com