PDO 准备语句:替换列的值

PDO Prepared Statements: Replacing the value of a column

如何使用准备好的语句和绑定参数来替换列中的值?例如,

$stmt = "UPDATE users SET name = :name
    WHERE name = :name";

$stmt->bindParam(:name, $oldName);
$stmt->bindParam(:name, $newName);
$stmt->execute();

您不能在 prepare 语句中使用相同命名的绑定参数 您的参数名称必须与

一样唯一
$stmt = $connection->prepare("UPDATE users SET name = :newName
    WHERE name = :oldName");

$stmt->bindParam(':oldName', $oldName);
$stmt->bindParam(':newName', $newName);

您必须使用不同的占位符:

$stmt = $dbh->prepare("UPDATE users SET name = :newname
    WHERE name = :oldname")
$stmt->bindParam(':oldname', $oldName);
$stmt->bindParam(':newname', $newName);
$stmt->execute();