Otávio Barreto
Otávio Barreto

Reputation: 1558

Update multiples columns using PDO

How do i add multiples columns in pdo for update? this is what I am trying to do but I need to update multiple $_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';
}

Upvotes: 1

Views: 1340

Answers (2)

Otávio Barreto
Otávio Barreto

Reputation: 1558

This is how I solved it

$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(); 

Upvotes: 2

gmiley
gmiley

Reputation: 6604

What is it that is not working in your code? If you need to update multiple columns, you just need to include them in your update statement: update table1 set col1 = ?, col2 = ?, col3 = ? where id = ?; then assign parameter values for each one.

Upvotes: 2

Related Questions