1
I have a class object that appears like this on print_r();
Membros Object
(
[idMembro:Membros:private] =>
[dataCadastro:Membros:private] => 2019-01-10
[nome:Membros:private] => Cleonice P
[apelido:Membros:private] => cleo
[dataNascimento:Membros:private] => 1974-10-03
[telefone:Membros:private] => 3237216149
[celular:Membros:private] => 32988518043
[bairro:Membros:private] => São Francisco
[endereco:Membros:private] => Rua Francisco
[email:Membros:private] => [email protected]
[sexo:Membros:private] => Masculino
[estadoCivil:Membros:private] => Solteiro
[dataBatismo:Membros:private] => 2019-01-09
[bloqueado:Membros:private] => n
[batizadoFora:Membros:private] => n
[usuario:Membros:private] => caca
[senha:Membros:private] => aaaa
)
But dating,
[dataBatismo:Membros:private] => 2019-01-09
It may come empty,
[dataBatismo:Membros:private] =>
I have the function below populating the membership table
public function cadastrar( $Membro ) {
$dataBatismo = empty($Membro->getDataBatismo()) ? "NULL" : $Membro->getDataBatismo();
$string = "INSERT INTO membros (
dataCadastro,
nome,
apelido,
dataNascimento,
telefone,
celular,
bairro,
endereco,
email,
sexo,
estadoCivil,
dataBatismo,
bloqueado,
batizadoFora,
usuario,
senha)
VALUES (
'" . $Membro->getDataCadastro() . "',
'" . $Membro->getNome() . "',
'" . $Membro->getApelido() . "',
'" . $Membro->getDataNascimento() . "',
'" . $Membro->getTelefone() . "',
'" . $Membro->getCelular() . "',
'" . $Membro->getBairro() . "',
'" . $Membro->getEndereco() . "',
'" . $Membro->getEmail() . "',
'" . $Membro->getsexo() . "',
'" . $Membro->getEstadoCivil() . "',
" . $dataBatismo . ",
'" . $Membro->getBloqueado() . "',
'" . $Membro->getBatizadoFora() . "',
'" . $Membro->getUsuario() . "',
'" . $Membro->getSenha() . "'
)";
print "<pre>";
print_r($string);
print "</pre>";
return $this->conexao->query( $string ) == true ? true : false;
}
The problem is here:
$dataBatismo = empty($Membro->getDataBatismo()) ? "NULL" : $Membro->getDataBatismo();
More precisely here:
$Membro->getDataBatismo();
When the date is not empty, then you end up in trouble here,
" . $dataBatismo . ",
Which leaves a nonstring in query
and can only be null or string date
2019-10-01,
When the right thing would be:
"2019-10-01",
How to solve this?
If it is a string, why does it not have single quotes like all other columns in your table?
– Woss
I would make the same remark. Since birth date apparently works, baptism date should also be in the same syntax. Take a look at whether it is the lack of simple quotes in the string mount.
– Paulo Weverton
because if it is null, it cannot go with quotation marks to the query or it will NOT work However, if it is NOT null, it needs the quotation marks but I made some attempts to add the quotation marks only when it is not null and none of them worked.
– Carlos Rocha
But you’ve already played the
NULL
in quotes. Will end up resulting in a string anyway:[...] mo()) ? "NULL" : "$Membr [...]
. Incidentally, his getter should already be ready to returnNULL
in empty fields.– LipESprY
When the "NULL" that is still in PHP arrives in SQL it moves in NULL without the quotes there. Po cause concatenation " . $dataBatismo . "
– Carlos Rocha
You could use a reply that would fix ALL that code posted?
– LipESprY
It makes sense @Lipespry, because the ideal would be to use PDO
– Paulo Weverton
@Pauloweverton Rightly. Actually, I don’t know what it uses. But it’s object oriented. It could be PDO with the method
query()
or Mysqli with the methodquery()
... My idea is to transform this query into Prepared statement.– LipESprY
That. The assembly of the consultation would be more organized and would not have this kind of problem that it seeks the solution
– Paulo Weverton
yes Lipespry , would be
– Carlos Rocha
@Carlosrocha All right. I will formulate an answer and I return now.
– LipESprY
but in the whole system I do not use PDO because only work with mysql. More specifically mysqli
– Carlos Rocha
PDO accepts Mysql and several other database types. Mysqli, on the other hand, only works with Mysql (or mariadb that gives in the same)...
– LipESprY
I will have to study a little bit about PDO yet. But thank you!
– Carlos Rocha
@Pauloweverton takes a look at my answer and see if you have anything else to complement it. Vlw!
– LipESprY