SQL error while trying to make an UPDATE with PHP [closed]

-3
public function AlterarDoador(ClasseDoador $AlterarDoador) {
        try {
            $pdo = conexao::getInstance();
            $sql = "UPDATE doador SET nome=?,cpf=?,datadenascimento=?"
                    . "sexo=?,endereco=?,complemento=?,bairro=?,cidade=?"
                    . "estado=?,cep=?,email=?,senha=?,telefone=?,perfil=?,datacadastro=? "
                    . "WHERE iddoador = " . $AlterarDoador->getIddoador() . ";";
            $stmt = $pdo->prepare($sql);
            $stmt->bindValue(1, $AlterarDoador->getNome());
            $stmt->bindValue(2, $AlterarDoador->getCpf());
            $stmt->bindValue(3, $AlterarDoador->getDatadenascimento());
            $stmt->bindValue(4, $AlterarDoador->getSexo());
            $stmt->bindValue(5, $AlterarDoador->getEndereco());
            $stmt->bindValue(6, $AlterarDoador->getComplemento());
            $stmt->bindValue(7, $AlterarDoador->getBairro());
            $stmt->bindValue(8, $AlterarDoador->getCidade());
            $stmt->bindValue(9, $AlterarDoador->getEstado());
            $stmt->bindValue(10, $AlterarDoador->getCep());
            $stmt->bindValue(11, $AlterarDoador->getEmail());
            $stmt->bindValue(12, $AlterarDoador->getSenha());
            $stmt->bindValue(13, $AlterarDoador->getTelefone());
            $stmt->bindValue(14, $AlterarDoador->getPerfil());
            $stmt->bindValue(15, $AlterarDoador->getDatacadastro());            
            return $stmt->execute();
        } catch (PDOException $exc) {
            echo $exc->getMessage();
        }
    }
    
asked by anonymous 24.06.2016 / 19:35

1 answer

3

Gabriel, first let's imagine what a UPDATE would be, regardless of whether you use PHP to execute it. It would be something like:

UPDATE nome_da_tabela
   SET campo1 = valor_campo1,
       campo2 = valor_campo2,
       campoN = valor_campoN
 WHERE condicao_aqui;

You enter the table name , and in SET you can put 1 or more fields with your values for the update, separated by commas . And finally, with WHERE , puts the conditions to filter the records , otherwise the whole table will be updated.

Now let's see this code:

$sql = "UPDATE doador SET nome=?,cpf=?,datadenascimento=?"
                    . "sexo=?,endereco=?,complemento=?,bairro=?,cidade=?"
                    . "estado=?,cep=?,email=?,senha=?,telefone=?,perfil=?,datacadastro=? "
                    . "WHERE iddoador = " . $AlterarDoador->getIddoador() . ";";

We can already notice that in the change of the lines you forgot to separate the fields by comma. The correct one would look like this:

$sql = "UPDATE doador SET nome=?,cpf=?,datadenascimento=?,"
                    . "sexo=?,endereco=?,complemento=?,bairro=?,cidade=?,"
                    . "estado=?,cep=?,email=?,senha=?,telefone=?,perfil=?,datacadastro=?, "
                    . "WHERE iddoador = " . $AlterarDoador->getIddoador() . ";";

If after this fix continues with error, make sure you have entered values for all parameters , in the $stmt->bindValue lines.

If the error persists, change your question by entering it.

    
24.06.2016 / 20:22