PDO查询更新了不在查询中的日期时间列

时间:2019-03-19 15:34:15

标签: php pdo prepared-statement

PDO准备的更新语句以某种方式更新表中所选记录的datetime列,即使该特定的datetime列甚至不在查询中。

if(isset($_POST['editCriteria']))
{
    $value = $_POST['editCriteria'];

    $editusername = $value['editusername'];
    $hiddenUsername = $value['hiddenUsername'];
    $editfullname = $value['editfullname'];
    $editemail = $value['editemail'];
    $edituserlevel = $value['edituserlevel'];
    $editdivision = $value['editdivision'];
    $editdept = $value['editdept'];
    $editphone = $value['editphone'];

    try
    {
        $dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $update = $dbc->prepare("UPDATE users_edi SET username = :uname, 
        fullname = :fname, userlevel = :ulevel, email = :uemail, 
        division = :udivision, dept = :udept, phone = :uphone WHERE username = :hname");

        $update->execute([
            'uname' => $editusername,
            'fname' => $editfullname,
            'ulevel' => $edituserlevel,
            'uemail' => $editemail,
            'udivision' => $editdivision,
            'udept' => $editdept,
            'uphone' => $editphone,
            'hname' => $hiddenUsername
        ]);    

        if($update)
        {
            echo "Success: User has been updated.";
        }
    }
    catch(PDOException $e)
    {
        echo "Error: " . $e->getMessage();
    }
}

在数据库表中,有一个名为lastLoginDate的列正在更新为当前日期时间。

如果您在上面的更新语句中注意到,则该查询不包含lastLoginDate。

lastLoginDate甚至不在查询中时如何更新?

1 个答案:

答案 0 :(得分:0)

使用SHOW CREATE TABLE命令后,lastLoginDate列确实存在触发器。

CREATE TABLE `users_edi` (
`username` varchar(30) NOT NULL DEFAULT '',
`fullname` varchar(50) DEFAULT NULL,
`userlevel` tinyint(1) unsigned NOT NULL,
`ipaddress` varchar(30) DEFAULT NULL,
`email` varchar(150) DEFAULT NULL,
`entrydate` datetime DEFAULT NULL,
`division` varchar(35) DEFAULT NULL,
`password` varchar(32) DEFAULT NULL,
`userid` varchar(32) DEFAULT NULL,
`timestamp` int(11) unsigned NOT NULL,
`job_title` varchar(30) DEFAULT NULL,
`dept` varchar(50) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL,
`lastLoginDate` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, // <-- here
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

我将不得不问另一个有关如何删除此触发器的问题。