Show name of the person who is connected to the system

1

I am making a system of military sanctions from scratch. with PHP and MYSQL

Already install the user LOGIN system

My question is:

How do I show a welcome message with the name of the user who logged in?

THE FOLLOWING ERROR COMES OUT:

  

Warning: Missing argument 1 for bd_user (), called in   C: \ xampp \ htdocs \ Sanctions System \ home.php on line 24 and defined   in C: \ xampp \ htdocs \ Sanctions System \ functions.php on line 8

     

Notice: Undefined variable: user_id in C: \ xampp \ htdocs \ System   Sanctions \ functions.php on line 10

I use the following forms:

LOGIN FORM:

<?php session_start(); ?>
<html>
<head>
    <title>Formulario de Registro</title>
    <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
</head>
<body>
<?php include "php/navbar.php"; ?>
<div class="container">
<div class="row">
<div class="col-md-6">
    <h2>Login</h2>

    <form role="form" name="login" action="php/login.php" method="post">
      <div class="form-group">
        <label for="username">Nombre de usuario o email</label>
        <input type="text" class="form-control" id="username" name="username" placeholder="Nombre de usuario">
      </div>
      <div class="form-group">
        <label for="password">Contrase&ntilde;a</label>
        <input type="password" class="form-control" id="password" name="password" placeholder="Contrase&ntilde;a">
      </div>

      <button type="submit" class="btn btn-default">Acceder</button>
    </form>
</div>
</div>
</div>
    <script src="js/valida_login.js"></script>
</body>
</html>

LOGO FUNCTION:

<?php

if(!empty($_POST)){
if(isset($_POST["username"]) &&isset($_POST["password"])){
    if($_POST["username"]!=""&&$_POST["password"]!=""){
        include "conexion.php";

        $user_id=null;
        $sql1= "select * from user where (username=\"$_POST[username]\" or email=\"$_POST[username]\") and password=\"$_POST[password]\" ";
        $query = $con->query($sql1);
        while ($r=$query->fetch_array()) {
            $user_id=$r["id"];
            break;
        }
        if($user_id==null){
            print "<script>alert(\"Acceso invalido.\");window.location='../login.php';</script>";
        }else{
            session_start();
            $_SESSION["user_id"]=$user_id;
            print "<script>window.location='../home.php';</script>";                
        }
    }
}
}

?>

FUNCTION OF SHOWING USER NAME:

function bd_usuario($user_id)
{
$sql = "SELECT fullname FROM user WHERE id = '$user_id' LIMIT 1";
return $sql;
}

WELCOME FORM WHEN THE USER IS CONNECTED:

<?php
session_start();
if(!isset($_SESSION["user_id"]) || $_SESSION["user_id"]==null){
    print "<script>alert(\"Acceso invalido!\");window.location='login.php';     </script>";
}
$user_id=$_SESSION["user_id"];

?>
<?php include "php/navbar.php"; ?>
<?php include "funciones.php"; ?>

<html>
<head>
    <title>.: HOME :.</title>
    <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
</head>
<body>

<div class="container">
<div class="row">
<div class="col-md-6">
    <h2>Bienvenido</h2>
<?php
$usuario = bd_usuario();
echo "";
?>
</div>
</div>
<    /div>
</body>
</html>

MANY THANKS FOR YOUR HELP

    
asked by Victor Alejandro Alvarado Vilo 16.01.2017 в 04:06
source

1 answer

1

In the call to the function on the home.php page you are not including the user_id attribute, you must pass it as an argument.

Try putting the following code inside the function bd_usuario :

include "conexion.php";

$sql = "SELECT fullname FROM user WHERE id = '$user_id' LIMIT 1";
$fullname = null;

$query = $con->query($sql);
while ($r=$query->fetch_array()) {
    $fullname=$r["fullname"];
    break;
}

return $fullname;
    
answered by 16.01.2017 в 04:31