Create profile page according to user

Asked

Viewed 4,705 times

0

I want to make every user registered on my site have their own profile page, where all users can access it and see the information, such as name, email, date of birth, etc.. But I don’t know two things:

1st: How can I get the information present in the Mysql database table to be displayed? I’ve only got the email, but since I’m a beginner I don’t know exactly how to show with the rest of the information

2º: This is my biggest doubt. How do I create a page for each user? I’ve seen that it doesn’t have to be exactly "physical" but virtual. But I don’t know how I do it.

connect.inc (link to the 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");
   }
?>
  • Add an addslashes to your post to prevent sql Injection. http://php.net/manual/en/function.addslashes.php

  • I was going to answer the other question, but you deleted it, so I’m just going to give you a quick explanation. You first need to have a parameter in the URL indicating which user you will display the profile, example: perfil.php?usuario=3, in this case I will take user profile 3, hence just perform a query in your table like this: select * from cadastro where id= '3', your query will return an array with all user data of that profile, from there only display the data of it on the page.

  • Got it. How do I put the parameter in the URL? Thank you for the answer.

  • @lvcs I voted to reopen, if you vote and 3 more then you answer normally

  • @Alexandreschleder just accessing or placing links going to URLS for example: meusite.com/perfil.php?usuario=3 or using friendly URL can have something like meusite.com/perfil/3

  • @diegofm does not think the question is appropriate, in my view it is wide, because it has several ways of doing, and yet I do not think it is well explained, was going to answer while it was open to help. But I was going to vote to close right after.

  • I’m not looking for a specific solution. I just wanted to be told some possible way of doing it. Being a beginner, I can’t be more specific than that, so it gets hard.

Show 2 more comments

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 simulating a database table, when accessing the page will be necessary the information of a unique identifier for the user, as for example we have here:

/users/23036/lvcs

Where 23036 is my user identifier.

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


Adapt to database

To catch the user from a database and not from a list follows these steps:

  • Delete list $usuarios.
  • Make connection to database.
  • Perform query in the user table where the identifier is equal to the URL.
  • Check if there was a 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 more secure. I haven’t used it here because I’m out of time.

Remarks

  • To 'put parameter in URL', as you asked in the comments, will depend a lot on the context of your system, usually is along with the user listing, you put the link of the profile page + the user identifier.
  • Thanks! I managed to make the profile page already, using the 2nd method with the database.

0

Do the following, you know the GET method? yes? perfect, what you will do is the following, you will create a page with the profile name.php, OK!. Soon after you will query this profile, example, mysql_query("SELECT * FROM usuarios WHERE id = 2"); and soon after you take a fetch_array() to display all the data of that user. Okay, but why the get method? Why can you have millions of users with thousands of different profiles I just try a profile file on your site. get will pass parameters, like. www.meusite.io/profile.php? id=3 <-- this id equals 3 will show the profile of the user who has id 3 in the database.

Sorry I don’t tell you the step by step how to do it, it is because it is a thing that has several ways to do, so if you are a beginner, I advise you to study a little database and get method. After that you will be able to make your profile page with peace of mind.

  • https://www.youtube.com/watch?v=Wyf2ejKRVb0 <-- watch this video and learn what you want to do.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.