Jump to content

Recommended Posts

Hello everyone! I am trying to insert a student into a table (with TIMESTAMP; works with VARCHAR, not TIMESTAMP). Can anyone help?

 

Variable

$time_stamp = date("D M j G:i:s T Y"); 

Populate DB Query

("DROP TABLE IF EXISTS enrolled") || !$link->query("CREATE TABLE enrolled(course_id VARCHAR(50), student_id VARCHAR(50), user_ip VARCHAR(50), time_stamp TIMESTAMP(6))

Insert Query

INSERT INTO enrolled(course_id,student_id,user_ip,time_stamp) VALUES('$course','$number','$user_ip','$time_stamp')
Edited by MatthewPatten

It looks like TIMESTAMP is formatted as "YYYY-MM-DD HH:MM:SS". More information can be found here:

http://dev.mysql.com/doc/refman/4.1/en/datetime.html

 

Also note that MySQL has some built-in functions for adding timestamps. Have you tried using NOW():

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_now

 

Note that you can find more time-related functions here:

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

 

It looks like TIMESTAMP is formatted as "YYYY-MM-DD HH:MM:SS". More information can be found here:

http://dev.mysql.com/doc/refman/4.1/en/datetime.html

 

Also note that MySQL has some built-in functions for adding timestamps. Have you tried using NOW():

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_now

 

Note that you can find more time-related functions here:

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

 

Finally got the NOW() function working!

Edited by MatthewPatten

The point of using a timestamp column is so you do not have to enter a datetime value - it is automatically entered.

 

For example

CREATE TABLE student (
  student_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(30),
  created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

INSERT INTO student (name) VALUES ('Barand');

Then

mysql> SELECT * FROM student;
+------------+--------+---------------------+
| student_id | name   | created             |
+------------+--------+---------------------+
|          1 | Barand | 2014-12-12 14:54:09 |
+------------+--------+---------------------+

 

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.