简体   繁体   中英

Display profile data from database when login

I'm pretty much a beginner to php since I've shift. My registration is working fine and log in(checking if its in the database). I'm getting confused on using the session(display user who's logged in).

This is my login.php : (working fine)

<?php
$Regusername = $_POST['username'];
$Regpassword = $_POST['password'];

$dbc = @mysql_connect('localhost' , 'root','');
@mysql_select_db('market', $dbc);

$query1 = "SELECT username FROM customer WHERE password='$Regpassword' and username='$Regusername'";

$r1 = @mysql_query($query1, $dbc); // user
$row1 = mysql_fetch_array($r1); // 

if ($row1['username'] == $Regusername && $Regpassword) { 
    session_start();
    $_SESSION['username']==$row1['username'];
    Header("Location: home.php");
  }
else
  { print '<p> <h1> Ooops, error login, please try again. </h1></p>'; }
?>

then in my profile.php. At the top I've already added " $link = mysql_connect('localhost', 'root', ''); "

<?php
  session_start(); // Start Session Data
  $username = $_SESSION['username'];
?>


<?php  
  echo "Name: ".$_SESSION['username']."" ; //Undefined index: username in this line, the error shows up. 
?> 

t only displays Name: Hope somebody helps me of what am I missing or should change here. Thank you so much in advance.

you never set the session:

$_SESSION['username']==$row1['username']; // is the same as 1=1 = true

that is a logical "query" meaning that session is row = true. however, it is supposed to be a statement where you set session to equal row. remove one of the equal signs.

also, this line looks rather confusing

if ($row1['username'] == $Regusername && $Regpassword)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM