Reputation: 2020
I am new to PHP and MySql. I am trying to insert simple form into MySql database. I have Apache/PHP/MySql in my machine. I am sorry if this is repetitive question. But i searched lot and not able to find the solution.
I have 3 html input fields 'name', 'age', 'sex' and inserting it into mysql with 4 columns in the insert query. The 4th column is 'datetime' column. Below are my structure.
$date = new DateTime();
$date = $date->format('Y-m-d H:i:s');
$sql_query = "insert into demoform (name, age, sex, Created_On)
values('$name', '$age', '$sex', '$date')";
$result = $db_conn->query($sql_query);
But after i do the insert, php form submitted successfully. But in the table, the date column show as "0000-00-00 00:00:00".
can anyone help me what am i doing wrong here?
*Question updated with the table structure...
mysql> show tables;
+-------------------+
| Tables_in_myphpdb |
+-------------------+
| demoform |
+-------------------+
1 row in set (0.00 sec)
mysql> describe demoform;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| sex | varchar(255) | YES | | NULL | |
| Created_On | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
Upvotes: 1
Views: 199
Reputation: 6661
use MySQL NOW()
$sql_query = "insert into demoform (name, age, sex, Created_On)
values('$name', '$age', '$sex', NOW())";
if you need current date time use MySQL now()
Upvotes: 1