简体   繁体   English

PHP MySQL更新返回成功,但未更改任何行

[英]PHP MySQL Update returns success, but no rows are changed

I have been trying to update my database through PHP but it's not working. 我一直在尝试通过PHP update数据库,但是无法正常工作。 Here is the function I am using. 这是我正在使用的功能。 This is all PHP 这就是全部PHP

function update($query, $values = "") {

        $success = false;
        $statement = $this->db->prepare($query);

        if (is_array($values)) {
            $success = $statement->execute($values);
            print("\nIt is an array");
        } else {
            $success = $statement->execute();
        }

        $statement->closeCursor();
        return $success;
}

And here is how I am calling it. 这就是我所说的。

$data=array();
    $data[] = $FirstName;
    $data[] = $LastName;
    $data[] = $Email;
    $data[] = $AccessLevel;
    $data[] = $UserID;

try{
    $thisDatabase->db->beginTransaction();
    $query ="UPDATE Users SET FirstName=?, LastName=?, ContactEmail=?, AccessLevel=? WHERE UserID=?";
    $userData = $thisDatabase->update($query,$data);
}catch (PDOExecption $e){
    $thisDatabase->db->rollback();
    print "There was a problem with the query";
}

This function returns 1 (when I print out $userData ). 此函数返回1 (当我打印出$userData )。

Here is the $data array that I am passing it. 这是我传递的$data数组。

Array(
    [0] => Jon
    [1] => Smith
    [2] => Jon@none.com
    [3] => 1
    [4] => 2
)

If I run the query in PHPmyadmin it works. 如果我在PHPmyadmin运行查询,它将起作用。 Note: I do have to add quotes to the columns stored as strings. 注意:我确实必须为存储为字符串的列添加引号。 UPDATE Users SET FirstName='Jon', LastName='Smith', ContactEmail='Jon@none.com', AccessLevel=1 WHERE UserID=2

So, does anyone have any ideas where my issue is or how I can go about finding it? 那么,是否有人对我的问题在哪里或如何找到它有任何想法?


For reference, I use a similar function for select statements, and it works perfectly. 作为参考,我对select语句使用了类似的功能,并且效果很好。

function select($query, $values = "") {

        $statement = $this->db->prepare($query);

        if (is_array($values)) {
            $statement->execute($values);
        } else {
            $statement->execute();
        }
        $recordSet = $statement->fetchAll(PDO::FETCH_NUM);

        $statement->closeCursor();
        return $recordSet;
}

You're not committing your transaction. 您没有提交交易。 Therefore, changes that you make with your UPDATE are not being actually saved in the database. 因此,您使用UPDATE所做的更改实际上并未保存在数据库中。

Add $thisDatabase->db->commit() after the try...catch block. try...catch块之后添加$thisDatabase->db->commit() This, of course, shouldn't be called if the transaction needs to be rolled back, so return from the function in the catch block, or add a flag. 当然,如果事务需要回滚,则不应调用此方法,因此请从catch块中的函数返回或添加标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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