Error when logging in when completing the registration

1

Community, the error I have is that the user to have finished the registration process I want to log it at once without the need to go to a login page, the code I am using for it is the following:

if(isset($_POST) && !empty($_POST)){
$names = $_POST['nombres'];
$pass = $_POST['password_user'];
$nie = $_POST['nie'];
$tlf = $_POST['telefono'];
$email = $_POST['email'];
$d_entrega = $_POST['address_entrega'];
$d_facturacion = $_POST['address_fact'];

$query = "INSERT INTO usuarios (nombres,pass,nie,telefono,direccion_entrega,direccion_facturacion,email)
VALUES ('$names','$pass','$nie','$tlf','$d_entrega','$d_facturacion','$email')";

$sql = $con->query($query);

if($sql>0){
    $query2 = "SELECT * FROM usuarios WHERE email = '$email' AND pass = '$pass'";
    $sql2 = $con->query($query2);
    $result = $sql2->num_rows;
    echo $result;
    $datos = $sql2->fetch_assoc();
    session_start();
    $_SESSION['usuario']=$datos;
    header('Location:productos.php');
}
}

The num_rows throws to me> 0 but when it does the redirection it sends me to the page without the initiated session. As if it were not logged.

    
asked by Alejo Mendoza 13.01.2018 в 03:09
source

1 answer

1

First of all, there was an error in the order of the fields of the insert which is a minor problem having as a maximum problem the insecure code that is being built. You should prepare your sentences.

If the session is not showing or has problems it was possibly because it was not started with session_start() , this debería realizarlo en los dos archivos y en todos los que emplee sesiones in the current one and in productos.php (at the beginning of the file)

Another consideration to consider is. Is it necessary to perform a select when you are sure that the user registered? . Maybe you can skip this step. (possible final code)

$query = "INSERT INTO usuarios (nombres,pass,nie,telefono,direccion_entrega,
           direccion_facturacion,email) VALUES (?,?,?,?,?,?,?)";
if ($stmt = $con->prepare($query)) {
    $stmt->bind_param("sssssss", $names ,$pass,$nie,$tlf,$d_entrega,$d_facturacion,$email);
    if($stmt->execute()){//Se inserto
        session_start();
        $_SESSION['carrito'] = array('email'=>$email,'pass'=>$pass);
        header('Location:script.php');
    }
}
    
answered by 13.01.2018 / 05:43
source