rookie2005 rookie2005 - 21 days ago 7
MySQL Question

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.

Answer

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)
Comments