Get the value of a php table

1

I am currently doing a php to make a login and redirect users, if the login matches and appears, then go to check if it is admin or not, for this, the query should return in the select the role, the doubt is, how do I get the role out and then compare it with an if?

<?php
//Login en la base de datos 
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";

//Coge las variables del ajax
$email = $_POST["email"];
$contraseña = $_POST['password'];
// Crea una nueva conexion
$conn = new mysqli($servername, $username, $password, $dbname);

// Comprueba la conexion
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$total = mysqli_num_rows(mysqli_query($conn," SELECT email FROM USUARIOS where email = '$email' and password = '$contraseña'"));
    if($total == 1){

        $admin = $conn,"SELECT role FROM USUARIOS where email = '$email' and password = '$contraseña'" ->get_result() ;
        if( $admin == 'admin'){
 echo "U are a admin user" . $admin;
            header('Location: ../admin.html');
        } else {
            echo "U are a normal user" . $admin; 
header('Location: ../user.html');
        }
    }else{
        echo "Error login";
    }

$conn->close();

?>

At the moment it is what I have done (I accept modifications or explanations also of other things since I am doing it as I can)

    
asked by Nicole 03.05.2018 в 11:13
source

1 answer

1

You could do everything in the same query, instead of dividing it in two.

Try changing this:

$total = mysqli_num_rows(mysqli_query($conn," SELECT email FROM USUARIOS 
where email = '$email' and password = '$contraseña'"));
if($total == 1){

    $admin = $conn,"SELECT role FROM USUARIOS where email = '$email' and 
password = '$contraseña'" ->get_result() ;
    if( $admin == 'admin'){
echo "U are a admin user" . $admin;
        header('Location: ../admin.html');
    } else {
        echo "U are a normal user" . $admin; 
header('Location: ../user.html');
    }
}else{
    echo "Error login";
}

for this:

$query = "SELECT role FROM USUARIOS where email = '$email' and password = '$contraseña'";

$result = mysqli_query($conn,$query);
//num_rows regresa el numero de filas del resultado del query.
if($result->num_rows == 1){
//fetch_assoc regresa el resultado como un associative array, o diccionario.
$row = mysqli_fetch_assoc($result);
$role = $row['role'];
if($role == 'admin') {
    echo "U are a admin user". $role;
    header('Location: ../admin.html');
} else {
    echo "U are a normal user" . $role;
    header('Location: ../user.html');
}

} else{
   echo "Error login";
}

I have not tried it, but it should be something like that.

    
answered by 03.05.2018 / 11:44
source