I'm new to PHP and am currently still learning. I'm having issues with my registration form I think. username
, password
, email
all insert into MySQL successfully. registered
and last_seen
do not.
I thought I was using getTimestamp()
wrong, but it echos what I need. However when I try to insert both timestamp fields into MySql, I see 0000-00-00 00:00:00
in those fields instead of what it echoed before the sql query. What am I doing wrong here? Any help is much appreciated.
$date = new DateTime();
$time = $date->getTimestamp();
echo '<div class="box_grey">' . $time . '</div>';
$sql = ' INSERT INTO users (username, password, email, registered, last_seen)
VALUES (:username, :password, :email, :registered, :last_seen)';
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $password);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':registered', $time);
$stmt->bindParam(':last_seen', $time);
$stmt->execute();
print '<div class="box_grey">errorCode() ' . $stmt->errorCode() . '<br /><br />';
print_r($stmt->errorInfo());
echo '</div>';
echo '<div class="box_grey">Registered successfully!</div>';
Here's what SHOW CREATE TABLE users;
shows me.
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'User''s unique ID number',
`username` varchar(16) NOT NULL,
`password` varchar(100) NOT NULL,
`email` varchar(254) NOT NULL,
`registered` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`last_seen` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`))
ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8
The obvious way to fix the error is to change the formatting of your value into the format that MySQL can accept. But rather than editing the value manually, you can use the STR_TO_DATE() function to help you convert the string value into date value.
MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
Internally, a MySQL timestamp column is stored as UTC. But, when you select a date, MySQL automatically converts the timestamp column to the current session's time zone. MySQL converts TIMESTAMP values from the current time zone to UTC for storage.
TIMESTAMP has a default of 0 unless defined with the NULL attribute, in which case the default is NULL .
That occurs pretty often.
You're confusing mysql timestamp which is actually a DATETIME like value with UNIX timestamp, which is number of seconds passed since 1970-01-01.
You need to either change field format or the inserting value.
Also you can find some mysql functions handy - CURDATE() or NOW () for example
$sql = 'INSERT INTO users VALUES (NULL,:username,:password,:email,NOW(),NOW())';
$stmt = $pdo->prepare($sql);
$stmt->execute(array($username, $password, $email));
But personally I'd avoid mysql timestamps. they can be changed unexpectedly, ruining all your data. I'd use DATETIME
and set all values manually.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With