Create profile page according to user

0

I want to make every user registered on my site have their own profile page, where all users will be able to access it and see the information, such as name, email, date of birth, etc. I know 2 things:

1st: How do I get the information in the MySQL database table to be displayed? I already got only the email, but as I am beginner I do not know exactly how to show with the rest of the information

2nd: That is my biggest doubt. How do I create a page for each user? I have already seen that it does not have to be exactly "physical" but virtual. But I do not know how.

connect.inc (link to database)

<?php
$dbservername = 'localhost';
$dbusername = 'root';
$dbpassword= '';
$dbdatabase = 'usuarios';

    $connect = mysqli_connect ($dbservername, $dbusername, $dbpassword, $dbdatabase);

    if (mysqli_connect_errno()) {


    echo "Failed to connect to MySQL: " . mysqli_connect_error();

}

?>

login.php

<?php

include "connect.inc";
session_start ();

if (isset ($_POST['login'])) {

    $email = mysqli_real_escape_string ($connect, $_POST['email']);
    $senha = mysqli_real_escape_string ($connect, $_POST ['senha']);

    $sel_user = "select id from cadastro where email = '$email' AND senha = '$senha'";
    $run_user = mysqli_query ($connect, $sel_user);
    $row = mysqli_fetch_array($run_user,MYSQLI_ASSOC);
    $active = $row ['active'];

    $check_user = mysqli_num_rows($run_user);

    if ($check_user == 1 )  {

        $_SESSION ['login_user'] = $email;

        header ("location: capa.php");
    }

    else {

        echo "Email or password is not correct, try again’";

    }


}

?>

session.php

<?php
include ("connect.inc");
session_start ();
$user_check = $_SESSION ['login_user'];
  $ses_sql = mysqli_query($connect,"select email from cadastro where email = '$user_check' ");

   $row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);

   $login_session = $row['email'];

   if(!isset($_SESSION['login_user'])){
      header("location:login.html");
   }
?>
    
asked by anonymous 06.01.2017 / 05:14

2 answers

0

I made a basic example without using a database so that any member can get something good:

<?php

$usuarios = [
    1 => ['nome' => 'Josue Ramos',      'idade' => 54,  'ocupacao' => 'ambulante' ],
    3 => ['nome' => 'Leonardo V.',      'idade' => 20,  'ocupacao' => 'desenvolvedor' ],
    4 => ['nome' => 'Patricia S.',      'idade' => 20,  'ocupacao' => 'quimica' ],
    5 => ['nome' => 'Figaldo M.' ,      'idade' => 34,  'ocupacao' => 'medico' ],
    7 => ['nome' => 'Ana Maria'  ,      'idade' => 45,  'ocupacao' => 'cozinheira' ],
];

if( isset($_GET['usuario']) ) {
    $usuario_id = (int)$_GET['usuario'];

    // faria aqui o: SELECT * FROM usuarios WHERE id = $usuario_id 

    if( array_key_exists($usuario_id, $usuarios) )  // no meu caso esse seria meu   SELECT
        $usuario = $usuarios[$usuario_id];
    else
        die('Usuario nao encontrado');
}
else
    die('Usuario nao encontrado');

?>

<h2>Perfil de '<?php echo $usuario['nome'] ?>'</h2>

<p>Tem <?php echo $usuario['idade'] ?> anos de idade.</p>

<p>Ocupa o cargo de <?php echo $usuario['ocupacao'] ?>.</p>

Explanation

In this example I have a array of users that simulates a table of the database, when accessing the page it will be necessary the information of a unique identifier for the user, as for example we have here:

http://pt.stackoverflow.com/users/23036/lvcs

Where 23036 is my user identifier.

When accessing the page, we need to get this identifier that was passed to us and display information about the user belonging to it, in that part I get the $_GET['usuario'] and I make an query in my array , which in my case simulates the database of data, if that user exists, I write it to a variable to perform the display, if it does not exist, I will kill the page with any error message.

Adapt to database

To get the user from a database and not from a list follow these steps:

  • Delete list $usuarios .
  • Make a connection to a database.
  • Query in the user table where the identifier is the same as the URL.
  • Check for true result.
  • Store in variable $usuario .

Leaving the code more or less like this (using mysqli_* ):

if( isset($_GET['usuario']) ) {
    $usuario_id = (int)$_GET['usuario'];
    $usaurio = null;

    // faria aqui o: SELECT * FROM usuarios WHERE id = $usuario_id 

    $connect = mysqli_connect ('localhost', 'root', 'root', 'lrv_001');
    $sel_user = "select * from users where id = '$usuario_id'";
    $run_user = mysqli_query ($connect, $sel_user);
    $usuario = mysqli_fetch_array($run_user, MYSQLI_ASSOC);

    if($row == $usuario)
        die('Usuario nao encontrado');
}
else
    die('Usuario nao encontrado');
  • Search for bind , to make the system safer. I did not use it here because I'm out of time.

Observations

  • To 'put parameter in URL', as you asked in the comments, it will depend a lot on the context of your system, it is usually along with the user list, you put the link of the profile page + the user identifier. / li>
09.01.2017 / 15:16
0

Do the following, do you know the GET method? yea? perfect, what you will do is the following, you will create a page with the name profile.php, OK !. Soon after you will do a query in this profile, eg mysql_query ("SELECT * FROM users WHERE id = 2"); and then you get a fetch_array () to display all that user's data. But why the get method? why you can have millions of users with thousands of different profiles try only one profile file on your site. the get will pass parameters, type.

I'm sorry not to mention the step-by-step how to do it, that's why there are several ways to do it, so if you're a beginner, I advise you to study a little database and the get method. After that you will be able to make your profile page with peace of mind.

    
09.01.2017 / 12:40