How can I display specific data from a MySQL database? (fetching user's email)

Legendary Jaguarz

I have made a registration system with a my account page.

I can display name and password in it but cannot display email after login because I have to fetch it from MySQL database as the user doesn't enter his email at login.

No error in code but email not displayed.

This is server.php login code:

// LOGIN USER 
if (isset($_POST['login_user'])) {
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $password = mysqli_real_escape_string($db, $_POST['password']);

  if (empty($username)) {
   array_push($errors, "Username is required");
  }
  if (empty($password)) {
    array_push($errors, "Password is required");
  }

  if (count($errors) == 0) {
    $password = base64_encode($password);
    $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
    $results = mysqli_query($db, $query);
    if (mysqli_num_rows($results) == 1) {
      $_SESSION['username'] = $username;

//Here is the code with problem

      $query = "SELECT email FROM users WHERE username='$username' AND password=''$password";

      $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        $_SESSION['email'] = $row["email"];
    }
    }

//Here it ends

      $_SESSION['password'] = $password;
      $_SESSION['success'] = "You are now logged in";
      if ($_SESSION['page'] == "account.php"){
          header('location: account.php');
      }
      else{
        header('location: index.php');
      }
    }else {
        array_push($errors, "Wrong username/password combination");
   }
  }
}
salvatore

You have an error in your second query, you have not quoted the password correctly. That said, you don't need the second query at all, because the email field is already in the first result. And you know it's only one row because you check mysqli_num_rows($results) == 1

if (count($errors) == 0) {
    $password = base64_encode($password);
    $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
    $results = mysqli_query($db, $query);
    if (mysqli_num_rows($results) == 1) {
        $user_data = mysqli_fetch_assoc($results);
        $_SESSION['username'] = $username;
        $_SESSION['email'] = $user_data['email'];           
        $_SESSION['password'] = $password;
        $_SESSION['success'] = "You are now logged in";
        if ($_SESSION['page'] == "account.php"){
            header('location: account.php');
        }
        else{
            header('location: index.php');
        }
    }else {
        array_push($errors, "Wrong username/password combination");
    }
}

As a last note, remember that base64 is not the ideal function to store password, and it's probably a good idea to use some hashing function instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Vaadin: How do I display data from a MySQL database?

How can I change color of specific card in recyclerview after receiving certain data from mysql database column

How do I lock down Firebase Database to any user from a specific (email) domain?

How can i show Data from mysql specific month

NodeJS + Mongo - How can I interact with my DOM fetching data from database?

How can I set a user's display name and email with Firebase signInWithCredentials()

How can I display the email of the user who has booked

Fetching specific data from MySQL table

Fetching data from mysql database with php

PHP Fetching Data From Specific MySQL Row

How can I migrate data from MySQL without the login to the database?

How can I display only specific data in my gridview considering other database tables field in my database

Fetching data issue from mysql database in this code

How can I send fetching data from database via mail

Fetching data from mySql database for php user session

How can I display data from a SQL database to a textbox

Fetching data from MySQL database to accordion

How to display "Fetching data" text to user while controller code is fetching data from API?

how can i get a specific data for a user from firebase in swift?

how can i retrive data from database and display it in a Select dropdown

How do i display data by ID from mysql database

how i can display data fetching from api in flutter

How can I create a text input with autocomplete and then display data from mysql database in express?

How can i get a specific data from Firbase database

How can I pass props after fetching from database?

Retrieving User's data from database on the basis of email

How can I only display the date from MySQL database?

How can I display data from MYSQL database using HTML table from a PHP file?

How can I fetch a specific array from the JSON data and display it?