如何在pdo中添加多列以进行更新?这是我想要做的,但我需要更新多个 $ _ POSTS [' VARS];
$consulta = $conexao_pdo->prepare('UPDATE user SET nome = ? WHERE id = ?');
$consulta->bindParam(1, $variavel_com_nome);
$consulta->bindParam(2, $id);
if ($consulta->execute()) {
echo 'UPDATED';
}
答案 0 :(得分:2)
什么是代码中无效?如果需要更新多个列,只需将它们包含在更新语句中:update table1 set col1 = ?, col2 = ?, col3 =?其中id =?;然后为每一个分配参数值。
答案 1 :(得分:0)
这就是我解决它的方式
$sql = "UPDATE user SET name = :name,
surname = :surname
WHERE username = :username";
//db column and value
$stmt = $conexao_pdo->prepare($sql);
//where clause
$stmt->bindParam(':username', $username);
//add vars to db
$stmt->bindParam(':name', $var);
$stmt->bindParam(':surname', $var);
$stmt->execute();