PDO query updating a datetime column not in query

2019-08-20 01:29发布

问题:

A PDO prepared update statement is somehow updating the datetime column for the selected record in the table, even though that particular datetime column is not even in the query.

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();
    }
}

In the database table, there is a column called lastLoginDate that is being updated to the current datetime.

If you'll notice in the update statement above, the query does not include lastLoginDate.

How is lastLoginDate being updated when it's not even in the query?

回答1:

Upon using the SHOW CREATE TABLE command, there was indeed a trigger on the lastLoginDate column.

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

I will have to ask another question on how to remove this trigger.