0
I’m trying to create a simple screen of user registration, but I’m having problems performing the insert
. The method is executed but the MySQL
inserts null
instead of form fields I get through a foreach
, can you tell me where I’m going wrong?
I’m using PDO Connection.
Usuario.php
<?php
Class Usuario{
private $id_login;
private $usuario;
private $senha;
private $status;
private $nome;
private $sobrenome;
public function __construct(){
//
}
public function getId_login(){
return $this->id_login;
}
public function setId_login($id_login){
return $this->id_login = $id_login;
}
public function getUsuario(){
return $this->usuario;
}
public function setUsuario($usuario){
return $this->usuario = $usuario;
}
public function getSenha(){
return $this->senha;
}
public function setSenha($senha){
return $this->senha = $senha;
}
public function getStatus(){
return $this->status;
}
public function setStatus($status){
return $this->status = $status;
}
public function getNome(){
return $this->nome;
}
public function setNome($nome){
return $this->nome = $nome;
}
public function getSobrenome(){
return $this->sobrenome;
}
public function setSobrenome($sobrenome){
return $this->sobrenome = $sobrenome;}
} // fim da classe usuario
?>
usuario_crud.php
<?php
Class Usuario_crud{
public static $conexaoPDO;
// Instanciando a conexao e a classe
public function __construct(){
$this->conexaopdo = Conexao::getConexao();
} public function Inserir(Usuario $usuario){
try{
// variavel recebendo a consulta
$insert = "INSERT INTO tbl_login_admin (
usuario,
senha,
nome,
sobrenome)
VALUES(
:usuario,
:senha,
:nome,
:sobrenome)";
// abrindo a classe de conexão PDO
$prepare_sql = $this->conexaopdo->prepare($insert);
// repassando parametros através de bindParam
$prepare_sql->bindValue(":usuario", $usuario->getUsuario());
$prepare_sql->bindValue(":senha", $usuario->getSenha());
$prepare_sql->bindValue(":nome", $usuario->getNome());
$prepare_sql->bindValue(":sobrenome", $usuario->getSobrenome());
// executando a instrução
return $prepare_sql->execute();
} // fim do try
catch (Exception $e) {
Echo " $e Ocorreu um erro ao tentar executar essa Inclusão de Dados, Tente novamente mais tarde
ou contato o administrador do Sistema";
}
} // fim da function Insert
?>
// here and where I go through the form fields through the foreach to pass to the Insert method
<?php
if (isset($_POST['cadastrar'])){
//laço que percorre os campos para obter os dados digitados no HTML através da Funçao $_POST
foreach ($_POST as $campo => $valor) {
$$campo = post($campo);
}
// instanciando a classe usuario.
$user = new Usuario();
// instanciando a classe usuario crud
$user_crud = new Usuario_crud();
// iniciando o metodo de Inserir
$user_crud->inserir($user, $campo);
} // fim do if
?>
you saw that
$$campo = post($campo);
has 2$
?– Jeferson Almeida
Nowhere did you set the properties of the object
$user
. Nor does a method make much senseset()
return value.public static $conexaoPDO;
doesn’t seem to be used for anything in the code.– rray
Hello friend, answering your question, I did see that the variable $$field has 2 $$ .. so actually as it is in the loop so it automatically gets the name fields of html, if you look at the foreach then I have a function that removes the '' from the field.. about the part of the connectionp is it q starts, if I take that chunk of the code Insert does not work. thanks for the help.. guys! now ok
– Diego Lela