Do not log in even if the data is correct - PHP

0

I have a form to "login" the client.

With two fields: user and password.

Is the syntax for connecting to the database correct?

 $conexion = mysqli_connect("mysql:host=localhost", "1718d0_osmar", "1718d0_osmar", "1718d0_osmar");

Why do I always get "Your data is not correct!" Although the data I enter are correct?

HTML Code:

<!DOCTYPE html>
<html lang="es">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Formulario de Login - Cliente</title>
        <link rel="stylesheet" href="css/estilos_iniciar_sesion.css">
        <script type="text/javascript" src="js/validar_iniciar_sesion_cliente.js"></script>
    </head>

    <body background="imagenes/fondo_campo2.jpg">
        <form name="miformulario" id="miformulario" action="logueo_cliente.php" method="POST" class="form" onsubmit="return validar_iniciar_sesion_cliente();">
            <h2>CLIENTE</h2>
            <h4>INICIAR SESIÓN</h4>
            <input type="text" placeholder="&#128272; Usuario" name="email" id="email" tabindex="1">
            <input type="password" placeholder="&#128272; Contraseña" name="password" id="password" tabindex="2">
            <div id="mensaje" align="center">
                <?php if(isset($_GET['error']) && $_GET['error'] == 'true'): ?>
                    <h4><p style="color:red;">¡Sus datos no son correctos!</p></h4>
                <?php endif; ?>
            </div>
            <div id="iniciar" align="center">
                <input type="submit" value="Iniciar sesión"><br/>
                <p class="form-link">¿Aún no tienes una cuenta? <a href="registrar.php">Regístrate aquí</a></p><br/>
                <p class="form-link"><a href="recuperar_contrasena.php">He olvidado mi contraseña</a></p><br/><hr/><br/>
                <p class="form-link"><img src="imagenes/entrar.jpg" height="10px" width="10px" alt="invitado"/>&nbsp;<a href="index.php">Entrar como invitado</a></p>
            </div>
        </form>
    </body>
</html>

PHP Code:

<?php
    //Iniciar una nueva sesión o reanudar la existente.
    session_start();

    //Guardamos el valor de los campos <input> del formulario en variables.
    $idemail = $_POST["email"];
    $password = $_POST["password"];

    //Nos conectamos a la base de datos.
    $conexion = mysqli_connect("mysql:host=localhost", "1718d0_osmar", "1718d0_osmar", "1718d0_osmar");
    $sql = "SELECT * FROM CLIENTES WHERE idemail='".$idemail."' and password='".$password."'";
    $resultado = mysqli_query($conexion, $sql);

    //Guardamos la cantidad de filas que hemos obtenido de la consulta.
    $filas = mysqli_num_rows($resultado);

    //Si existen filas...
    if($filas>0){
        //Hay un cliente logueado correctamente y cargamos "index.php".
        $_SESSION["cliente"] = $idemail;
        header("Location: index.php");
    }else{
        header("Location: iniciar_sesion_cliente.php?error=true");
    }

    //Libera la memoria del resultado.
    mysqli_free_result($resultado);

    //Cierra la conexión.
    mysqli_close($conexion);
?>
    
asked by omaza1990 11.01.2018 в 00:28
source

1 answer

1

Several problems with the code:

Check that the base is connected:

$conexion = mysqli_connect("127.0.0.1", "1718d0_osmar", "1718d0_osmar", "1718d0_osmar");

if(!$conexion){
 exit("Error!!" . mysqli_connect_error() );
}

Escape your inputs !!!:

$idemail= mysqli_real_escape_string($conexion, $_POST['email']);
$password= mysqli_real_escape_string($conexion, $_POST['password']);

You do not need a variable for a value that you'll use only once

You do not need to declare your variables explicitly when the string is delimited by ""

$resultado = mysqli_query($conexion, "SELECT * FROM CLIENTES WHERE idemail='$idemail' AND password='$password';");
if(mysqli_num_rows($resultado) > 0){
    $_SESSION["cliente"] = $idemail;
    header("Location: index.php"); 

} else{
    //header("Location: iniciar_sesion_cliente.php?error=true"); //probablemente el problema está en el query
    echo "El query es SELECT * FROM CLIENTES WHERE idemail='$idemail' AND password='$password';";
    exit();
}

If you still have problems, execute that direct query in the database with the client of your preference (phpmyadmin, navicat, etc.) so you can see the result of the query. It is likely that the data is not correct.

I hope this helps.

    
answered by 11.01.2018 в 20:06