Re: Inserting date into a table

From: Date: Sat, 24 Apr 2004 12:28:26 +0000
Subject: Re: Inserting date into a table
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-34068@lists.php.net to get a copy of this message
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

« previous php.db (#34068) next »