Display user data logged in php

1

I'm trying to show data that the user has connected, but I can not. What I want to show is the text of the alert that that connected user has. As you see and done a print_r and I get SELECT alerta FROM usuarios WHERE usuario='[email protected]' , the user I have assigned to the user email. I get the connected user, but does not return that user's alert.

This is the SOLUTION, NO ONE thought about what I said that the user of the session was linked to the email:

<?php session_start();

include_once '../pruebas/conexion.php';
if (!isset ($_SESSION['usuario'])){
    header('Location: ../login.php');
};


$sql_leer = "SELECT alerta FROM usuarios WHERE email='".$_SESSION['usuario']."'";


$gsnet = $pdo->prepare($sql_leer);
$gsnet->execute();

$resultado = $gsnet->fetchAll();



?>



                            <div class="alert alert-danger" role="alert">

                            <?php echo $resultado?>

                              </div>
    
asked by 21.07.2018 в 21:05
source

1 answer

0

There are several causes why the code may be failing. It may be the prepared query that is not written correctly, it may be that there is no data match, etc.

The code can be improved at several points:

  • The query is not secure
  • It does not make sense to use fetchAll if it is a single column, it is preferable to use fetchColumn
  • It does not make sense to use a loop if you wait a single row
  • I have used a single block of code (PHP) concatenating a variable. That avoids the ugly mix of PHP / HTML.

The code would be this:

<?php 
    session_start();
    if (!isset ($_SESSION['usuario'])){
        header('Location: ../login.php');
    };

    $usuario=(!empty($_SESSION['usuario'])) ? $_SESSION['usuario'] : NULL;
    if ($usuario){
        include_once '../pruebas/conexion.php';
        if($pdo){
            $sql_leer = "SELECT alerta FROM usuarios WHERE usuario=?";
            if ($gsnet = $pdo->prepare($sql_leer)){
                if ($gsnet->execute([$usuario])){
                    $resultado = $gsnet->fetchColumn();
                    $strHTML=($resultado) ? '<div class="alert alert-danger" role="alert">'.$resultado.'</div>' : "No hay datos con el criterio buscado";
                }else{
                    $strHTML="Error en la ejecución de la consulta: ".$gsnet->errorInfo()[2];
                }
            }else{
                $strHTML="Error preparando la consulta: ".$pdo->errorInfo()[2];
            }
        }else{
            $strHTML="No existe conexión a la base de datos";
        }
    }else{
        $strHTML="No existe usuario en la variable de sesión";
    }
    echo $strHTML;
?>

It can still be refined, controlling for example that the preparation of the query was done well or capturing the errors. Also, when there are no results fetchColumn returns FALSE , a control could be established to define what to do in those cases, which are also possible when the criterion is not met.

    
answered by 22.07.2018 в 02:04