SQL error while trying to Update with PHP

Asked

Viewed 61 times

-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();
        }
    }
  • 4

    Gabriel well came to stackoverflow, make a tour and see how to create questions, post the error that is occurring.

  • 1

    Welcome to the site. In the title of your question, you state that there was an SQL error. What error?

  • 3

    There’s a syntax error, missing , in some fields look at line breaks

  • SQLSTATE[42000]: Syntax error or access Violation: 1064 You have an error in your SQL syntax; check the manual that Corresponds to your Mariadb server version for the right syntax to use near '' at line 1

  • that was the mistake

1 answer

3

Gabriel, first let’s imagine what a UPDATE, regardless of using the PHP to run it. It would be something like:

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

You inform the table name, and in the SET can put 1 or more fields with their respective values for the update, comma-separated. And finally, with the WHERE, places the conditions for filtering records, otherwise the whole table will be updated.

Now let’s see that code of yours:

$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 when changing the lines you forgot to separate the fields by comma. The correct one would be so:

$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 correction still error, check whether you are values for all parameters, on the lines $stmt->bindValue.

If the error persists, change your question by stating the same.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.