gpt4 book ai didi

php - 奇怪的 pdo sql 更新行为

转载 作者:行者123 更新时间:2023-11-29 19:02:10 25 4
gpt4 key购买 nike

我在 PDO 中有一个 sql UPDATE 查询,它应该更新名称和权限字符串。但它只是将该行的 id 放置在所有列中。

这是我的代码:

public function saveRole($roleID, $name, $permissions)
{
$sql = "UPDATE roles SET name = :name, permissions = :permissions WHERE id = :id";

//this one sets a string variable in the PDO wrapper class
PDO::setSQL($sql);

//this one sets an array inside the PDO wrapper class
PDO::setData([
'name' => $name,
'permissions' => $permissions,
'id' => $roleID,
]);

PDO::execute();

return PDO::getResponse(true);
}

如您所见,我为 PDO 编写了一个包装器,如下所示:

static function execute($sql = null, $data = null, $fetchmode = \PDO::FETCH_OBJ)
{
//check if data and SQL are set in function call, if so, use function call params, if not use class params ($this->SQL & $this->data)
self::connect();

try
{
$stmnt = self::$con->prepare(self::$sql);

$stmnt->setFetchMode($fetchmode);

if (sizeof(self::$data) > 0)
{
foreach (self::$data as $key => $value)
{
$stmnt->bindParam(':' . $key, $value);
}
}

$stmnt->execute();

self::$stmnt = $stmnt;

self::$data = [];
self::$sql = '';

self::$lastResponse = new pdoReturn(true, $stmnt);

return;
} catch (\PDOException $exception)
{
self::$data = [];
self::$sql = '';

self::$lastResponse = new pdoReturn(false, $exception);

return;
}
}

function setSQL($sql) {
if (!is_string($sql))
return false;
if (strlen($sql) == 0)
return false;
$this->sql = $sql;
return true;
}

function setData($data) {
if (!is_array($data))
return false;
$this->data = $data;
return true;
}

最佳答案

As you see, I've written a wrapper for PDO

要立即修复,请更改

   $stmnt = self::$con->prepare(self::$sql);
$stmnt->setFetchMode($fetchmode);

if (sizeof(self::$data) > 0)
{
foreach (self::$data as $key => $value)
{
$stmnt->bindParam(':' . $key, $value);
}
}

$stmnt->execute();

    $stmnt = self::$con->prepare(self::$sql);
$stmnt->setFetchMode($fetchmode);
$stmnt->execute(self::$data);

然后阅读 your first database wrapper's childhood diseases并修复其他问题,例如状态和错误报告。

关于php - 奇怪的 pdo sql 更新行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43786217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com