简体   繁体   中英

trigger default mysql datetime value when not null with php pdo

I'm trying to learn PDO and apply to an existing Joomla database table ("users"), just for learning purposes. However I keep getting errors with regard to the "lastResetTime" column.

23000 SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'lastResetTime' cannot be null

When the default system makes a user the default value is set to 0000-00-00 00:00:00, although the settings are NOT NULL.

For some reason I cannot pass this value (0000-00-00 00:00:00) in my queries. This is what I have. Could anyone tell me how I can trigger the default value, despite the facct that I cannot provide NULL, nor 0000-00-00 00:00:00 (which is strictly invalid if I'm correct)

<?php
require 'PDO.php';

$table = $prefix."users";

$pdo = new PDO($dsn,$user,$pass,$opt);

$stmt = $pdo->prepare("INSERT INTO $table (username, email, params, registerDate, lastvisitDate, lastResetTime) VALUES(?, ?, ?, now(),now(), ?)");
$stmt->execute(["lalaa","aaaa","",NULL]);

The table is formated as follows:

 CREATE TABLE `x7j4o_users` (
 `id` int(11) NOT NULL,
 `name` varchar(400) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
 `username` varchar(150) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
 `email` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
 `password` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
 `block` tinyint(4) NOT NULL DEFAULT '0',
 `sendEmail` tinyint(4) DEFAULT '0',
 `registerDate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
 `lastvisitDate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
 `activation` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
 `params` text COLLATE utf8mb4_unicode_ci NOT NULL,
 `lastResetTime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT      'Date of last password reset',
 `resetCount` int(11) NOT NULL DEFAULT '0' COMMENT 'Count of password resets since lastResetTime',
 `otpKey` varchar(1000) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT 'Two factor authentication encrypted keys',
 `otep` varchar(1000) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT 'One time emergency passwords',
 `requireReset` tinyint(4) NOT NULL DEFAULT '0' COMMENT 'Require user to   reset password on next login'
)     
ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

UPDATE The following doesn't seem to work also

$stmt = $pdo->prepare("INSERT INTO $table (name, username,registerDate,lastvisitDate,lastResetTime) VALUES(?,?,DEFAULT,DEFAULT,DEFAULT)");
$stmt->execute(['aaaa','aaaaaaaa']);

it triggers the error:

22007 SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00' for column 'registerDate' at row 1

If anyone knows how I can use the default value as set in mysql I would be very greatfull.

The answer has apparently something to do with PDO

The solution is the following line:

PDO::MYSQL_ATTR_INIT_COMMAND => "SET sql_mode=''"

Always great to waste 1 complete day ;)

In my code it is embeded here:

$opt = [
PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES   => false,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET sql_mode=''"
];

Although this fixed the problem, I'm hesitant to say it is a solid solution. If have read https://codeascraft.com/2013/03/19/the-perils-of-sql_mode/ which thought me the dangers of sql_mode set to null. This seems to be the architecture of the tables I use, which is supplied by Joomla CMS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM