1
I have a method that receives a model with some fields filled, I want to update only these fields, my current method is like this:
public function atualizar($aluno,$email)
{
$sql = new Sql();
if ($aluno->email == null){
$aluno->email = $email;
}
if ($aluno->senha != null){
$hash = Bcrypt::hash($aluno->senha);
$aluno->senha = $hash;
}
try {
return $sql->query("UPDATE tb_alunos SET nome= :nome,sobrenome=:sobrenome, email=:email,senha=:senha,unidadelocal=:unidadelocal,unidadecurricular=:unidadecurricular,diapreparacao=:diapreparacao,liberadoexercicio=:liberadoexercicio,token=:token,sub=:sub WHERE email=:email", $aluno);
} catch (\PDOException $e) {
echo $e;
return false;
}
}
It updates, using the model, but when there is no data in any field of the model, it overwrites the value that was null, and that’s the problem, I have to update only the fields passed
EDIT
UPDATE tb_alunos SET sobrenome=:sobrenome, unidadecurricular=:unidadecurricular WHERE email=:email
SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
@Igoroliveira tries to do with the edition I made at the end of the post.
– Andrei Coelho
does not work the query looks like this: UPDATE tb_students SET name=:, surname=:last name, unidadelocal=:, uniadecurricular=:unidadecurricular, diapreparacao=:, liberadoexercicio=:, token=:, sub=: WHERE email=:email
– Igor Oliveira
I think it would be interesting then to pass the copied object, and change only the data to update
– Igor Oliveira
@Igoroliveira I think so too! We are back to the first stage! rsrsrs
– Andrei Coelho
@Igoroliveira erased my edits because they will not help. good luck.
– Andrei Coelho
Thanks for trying
– Igor Oliveira