Hello i want 2add the name of the person who logged in at the top of the page, but the name comes from a SQL database called details, but the username and password is kept in another table called member they both are linked by id, this code below luks 2be right but it dont display the name 4 some reason can anybody help thanks

You are logged in as <?php echo $_SESSION; ?>. To log out <a href="homepage.html">click here</a>.

You need a database query on your login page to fetch the name you wish to display, then to register it in $_SESSION. Not possible to show you exactly how without seeing your login page code.


Matti Ressler
Suomedia

hello mate which login page? the 1 u fill the username and password or the code that links 2database

The code that links to database when you login.


Matti Ressler
Suomedia

<?php
$host="host"; // Host name 
$username="dk"; // Mysql username 
$password="d"; // Mysql password 
$db_name=""; // Database name 


// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// encrypt password 
$encrypted_mypassword=md5($mypassword);

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM members WHERE username='$myusername' and password='$encrypted_mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword"); 
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

Try this:

if($count==1){
// Register $myusername and redirect to file "login_success.php"
session_start();
$_SESSION['myusername'] = $myusername; 
header("location:login_success.php");
}

I dont see any reason to register the password.

You did mention using name from the other table - you can retrieve that based on id from $result and register it the same way.

Also, you should be sanitizing the user input before using it in database queries.

Matti Ressler
Suomedia

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.