Error logging php in mvc

1

Good evening everyone, after searching and searching (they are very much sticking with the code and look at things before asking for help), I resort to my first post on this page, because it is something that is bringing me head.

I am in the last year of the CFGS of DAW, and we are with a login and registration app with MVC, to "play around" with the design pattern, everything works perfect: register, delete, update ..., but , at the time of logging ... there is something that is failing me, and I have tried a thousand ways, I do not know why, I do not get the user that I want to consult (the one that initiates session).

UsersController.php

public function loguear(){

        $usuario = new Usuario();

            $idUsuario = $_POST['idUsuario']; //es el correo electronico
            $pass = $_POST['pass'];

            $usuario->setIdUsuario($idUsuario); //seteo el idUsuario
            $usuarioQueSolicitaLoguearse = $usuario->getIdUsuario(); //lo recojo
            $usuarioEnBaseDeDatos = $usuario->loguearUsuario($usuarioQueSolicitaLoguearse); //consulto a la base de datos el idUsuario introducido por el usuario

                if($usuarioQueSolicitaLoguearse == $usuarioEnBaseDeDatos){

                        echo "TODO OK!!!!!<br>";
                        echo "USUARIO SOLICITANTE: " .$usuarioQueSolicitaLoguearse."<br>";
                        echo "USUARIO EXISTENTE BBDD: " .$usuarioEnBaseDeDatos."<br>";  
                    // session_start();
                    // $_SESSION['usuarioLogueado'] = $usuarioQueSolicitaLoguearse;

                    //  echo $_SESSION['usuarioLogueado'];  
                    // redireccciono al controlador y el metodo logueo
                    // $this->redirect("Usuarios","logueo");
                    // $this->redirectWithSession("Usuarios","logueo",$_SESSION['usuarioLogueado']);
                }else{
                        var_dump($usuarioEnBaseDeDatos);
                        echo "<br>";
                        var_dump($usuarioQueSolicitaLoguearse);
                        echo "<br>";
                        echo "USUARIO NO ENCONTRADO<br>";
                        echo "USUARIO SOLICITANTE: " .$usuarioQueSolicitaLoguearse."<br>";
                        echo "USUARIO EXISTENTE BBDD: ".$usuarioEnBaseDeDatos."<br>";   
                        //redireccciono al controlador y el metodo logueoNotFound
                        // $this->redirect("Usuarios","logueoNotFound");
                }
    }

Users.php

public function loguearUsuario($idUsuario){

        $query = "SELECT idUsuario FROM usuarios WHERE idUsuario = ".$idUsuario;

        $resultado = $this->db()->query($query);

        return $resultado;


    }

In the browser, the result of the search in the bbdd I get false (result of the var_dump of UsersController.php), and as a consequence, the echo is empty, which, logically, does not make the comparison of the if.

View in the browser

  

bool (false) string (16) "[email protected]" USER NOT FOUND

     

APPLICANT USER: [email protected]

     

EXISTING USER BBDD:

asked by davidenco 25.05.2017 в 02:57
source

3 answers

0

I would do something like this:

public function loguearUsuario($idUsuario, $pass){

        $query = "SELECT idUsuario FROM usuarios WHERE idUsuario = ".$idUsuario . " AND password = '" . $pass . "'";

        $resultado = $this->db()->query($query);
        $num_rows = mysqli_num_rows($resultado );    

        if ($resultado == 0)
            return false;
        else 
            return true;
    }

public function log () {

$idUsuario = $_POST['idUsuario']; //es el correo electronico
$pass = $_POST['pass'];

$usuario = new Usuario();
if ($usuario->loguearUsuario($idUsuario, $pass)) {
     echo "BINGO !";
}
else {
     echo "NO NO NO";
}

Greetings,

    
answered by 25.05.2017 в 03:14
0

First, I see that the login logic is to verify if the username and password correspond to a user of your database so it should be something like this (I do not know how you generate the password)

Then

public function loguearUsuario($username, $password){

    $query = "SELECT * FROM usuarios WHERE username = '".$username."' AND password = '".$password."'";

    $resultado = $this->db()->query($query);

    return $resultado;
}

Then check for an answer

$result = $usuario->loguearUsuario($username, $password);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
          echo "id: " . $row["id"]. " - Nombre: " . $row["nombre"];
    }
}else {
    echo "Usuario o password incorrectos";
}
    
answered by 25.05.2017 в 03:13
0

Good guys, I found the error, and that is that I was comparing an object (which was what I returned loguearUsuario ()), with a String (which was what came from $ _POST ['idUsuario']), the solution It was this

    public function loguearUsuario($idUsuario)
{

    // $query = "SELECT idUsuario FROM usuarios WHERE idUsuario = ".$idUsuario;
    $query = "SELECT * FROM usuarios WHERE idUsuario = '" . $idUsuario . "'";
    $resultado = $this->db()->query($query);
    return $resultado->fetch_array(); //SOLUCION!!

}

In this way, it returns an array with the user's data with that id, and in the user's controller, I just have to play with the positions of the array that I get from loggingUser (), and using them at my whim

public function loguear(){

        $usuario = new Usuario();

            $idUsuario = $_POST['idUsuario']; //es el correo electronico
            $pass = $_POST['pass'];

            $usuario->setIdUsuario($idUsuario); //seteo el idUsuario
            $usuarioQueSolicitaLoguearse = $usuario->getIdUsuario(); //lo recojo
            $usuarioEnBaseDeDatos = $usuario->loguearUsuario($usuarioQueSolicitaLoguearse); //consulto a la base de datos el idUsuario introducido por el usuario

                if($usuarioQueSolicitaLoguearse == $usuarioEnBaseDeDatos[0] && $pass == $usuarioEnBaseDeDatos[1]){

                    session_start();
                    $_SESSION['usuarioLogueado'] = $usuarioEnBaseDeDatos[2]; //guardo el nick
                    $_SESSION['passUsuarioLogueado'] = $usuarioEnBaseDeDatos[1]; //guardo el pass


                    // redireccciono al controlador y el metodo logueo
                    $this->redirect("Usuarios","logueo");
                }else{

                        //redireccciono al controlador y el metodo logueoNotFound
                        $this->redirect("Usuarios","logueoNotFound");
                }
    }

Thank you for your answers, but I wanted to share the solution, so you do not think I am one of the many who enter here only to find a solution to the problem, and then they do not appreciate it.

Greetings !!

    
answered by 29.05.2017 в 12:53