PDO query updating a datetime column not in query

∥☆過路亽.° 提交于 2019-12-11 17:21:56

问题


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.



来源:https://stackoverflow.com/questions/55244684/pdo-query-updating-a-datetime-column-not-in-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!