how to register a new user setting SESSION as one of the query values

Asked

Viewed 25 times

0

i want to register a new User by a manager and setting the company id in the bd, the way I did it worked when registering other tables but this is giving problem but I know that the value of Session loads because I returned the values that were loaded in variables, but it just doesn’t register and shows no error...

-----------------controller ---------------------

<?php

    session_start();

require_once("../../model/usuarioModel/Usuario.php");
class ControllerCadastrarUser{

     private $cadastro;

    public function __construct(){
        $this->cadastro = new Usuario();
        $this->incluirUsuario();
    }

    private function incluirUsuario(){
        $this->cadastro->setNome_usuario($_POST['nome_usuario']);
        $this->cadastro->setTipo_usuario($_POST['tipo_usuario'] );
        $this->cadastro->setLogin($_POST['login'] );
        $this->cadastro->setSenha($_POST['senha'] );
        $this->cadastro->setId_empresa($_SESSION['empresa_id_empresa']);

        $result = $this->cadastro->incluirUsuario();
        if($result >= 1){
            echo "<script>alert('Registro incluído com sucesso!');document.location='../../view/empresa/cadastroEmpresa.php'</script>";
        }else{
            echo "<script>alert('Erro ao gravar registro!, verifique se email ou senha não estão errados!');history.back()</script>";
        }
    }
}
new ControllerCadastrarUser();



------------------- DAO  -------------

require_once("../../init.php");

class UsuarioDao {

    protected $mysqli;


    public function __construct(){
        $this->conexao();
    }

    private function conexao(){
        $this->mysqli = new mysqli(BD_SERVIDOR, BD_USUARIO , BD_SENHA, BD_BANCO);
    }

    public function setUsuario($nome_usuario, $tipo_usuario, $login, $senha,$id_empresa){

        $stmt = $this->mysqli->prepare("INSERT INTO usuario (`nome_usuario`, `tipo_usuario`, `login`, `senha`, `empresa_id_empresa`) VALUES (?,?,?,?,?)");
        $stmt->bind_param("sssss",$nome_usuario,$tipo_usuario,$login,$senha,$id_empresa);
         if( $stmt->execute() == TRUE){
            return true ;
        }else{
            return false;
        }

    }
}


------------------------ CLASSE USUARIO ---------------

require_once("UsuarioDao.php");
class Usuario extends UsuarioDao {
    private $nome_usuario;    
    private $tipo_usuario; 
    private $login; 
    private $senha; 
    private $id_empresa;


    function getNome_usuario() {
        return $this->nome_usuario;
    }

    function getTipo_usuario() {
        return $this->tipo_usuario;
    }

    function getLogin() {
        return $this->login;
    }

    function getSenha() {
        return $this->senha;
    }

    function getId_empresa() {
        return $this->id_empresa;
    }

    function setNome_usuario($nome_usuario) {
        $this->nome_usuario = $nome_usuario;
    }

    function setTipo_usuario($tipo_usuario) {
        $this->tipo_usuario = $tipo_usuario;
    }

    function setLogin($login) {
        $this->login = $login;
    }

    function setSenha($senha) {
        $this->senha = $senha;
    }

    function setId_empresa($id_empresa) {
        $this->id_empresa = $id_empresa;
    }

    public function incluirUsuario(){
        return $this->setUsuario($this->getNome_usuario(),$this->getLogin(), $this->getId_empresa(), $this->getTipo_usuario(), $this->getSenha());
    }

}

What should I do? Can you help me?

  • I may be wrong, but in your INSERT is set 5 columns, and you try to pass 6 values

1 answer

1

From the looks of it, your Insert is in trouble, because you are passing 5 columns and 6 values, soon one will be left.

A good method to test if your Sql code is working is to go straight to Phpmyadmin and test the code there in your table, because this way you can better check the errors presented, if it occurs.

After entering Phpmyadmin, just select the database you are using and then go to the "Sql" tab, there you will find the field to insert your Sql code. Simply replace variables with generic values.

Would look like this:

INSERT INTO usuario (`nome_usuario`, `tipo_usuario`, `login`, `senha`, `empresa_id_empresa`) VALUES ('nomeTeste','tipoTeste','loginTeste','senhaTeste', 1)"

Note: Note the type of data to be inserted (varchar, int, float, etc)

Browser other questions tagged

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