Jump to content

Recommended Posts

Hi.  Im trying to display on a page the details of the person that has logged in and only thier details.  Im starting nice and simple with trying to display just thier first name.  This is so i get a better understanding of how it is done.

 

Now the page i have made will only let you onto it as long as you have logged in on my signing in page.  I have a table called members.  I want to display a field called FirstName based upon thier login username which in my table is called UserName.  I am very new to this so can someone point out my errors in my code? 

 

<?php

session_start();

if (isset($_SESSION['Username'])) {

  $sql = "SELECT FirstName,data FROM Members WHERE id = UserName '{$_SESSION['FirstName']}'";

  // execute query and display the page.

} else {

  // user not logged in.

}?>

 

Also anybody know any good sites to learn php from as i am teaching myself at the minute?  Thank you for your help!

Link to comment
https://forums.phpfreaks.com/topic/113434-name-of-person-logged-in/
Share on other sites

first of all please use code tags for code.

second is there a session called FirstName

and your query is wrong

 

 

$sql = "SELECT FirstName,data FROM Members WHERE firstname = '{$_SESSION['FirstName']}'";
mysql_query($sql) or die("Error".mysql_error);

 

if your table is called Username , why are you calling data from a table called members?

 

and why are you selecting there details from where there id matches there username

 

and you are not running the query either

change $sql to this:

$sql = "SELECT FirstName,data FROM Members WHERE id = UserName '" . $_SESSION['Username'] . "'";

 

then add this after it:

$query = mysql_query($sql) or die("query failed " . mysql_error());
$row = mysql_fetch_array($query);

echo $row['FirstName'];

 

Regards ACE

Welcome to PHP! Its great stuff! Now there are lots of recources out there to help you on your way. To this day PHP.net is my best rescource for functions and general language questions.

 

Now on this that your are doing now. I will suggest this.

 

1. Start with a login form, where the user will input the username and password.

 

2. Post it to the same page, and then run the query ->

 

"SELECT FirstName,data FROM Members WHERE username='" . $_POST['username'] . "' AND password ='" . $_POST['password'] . '" LIMIT 1"

 

Then once you have a result you can load all of the data into the session vars that you need.

 

<?php

$result = mysql_query("SELECT FirstName,data FROM Members WHERE username='" . $_POST['username'] . "' AND password ='" . $_POST['password'] . '" LIMIT 1");

if(!$result)
{
    echo mysql_error();
}
else
{
   $row = mysql_fetch_array($result);

   $_SESSION['FirstName'] = $row['FirstName'];
}

?>

 

Now just to note this script is missing alot of stuff but you will be able to get the basic understanding I think... Let me know if you have any questions.

 

:D

 

and again Welcome to PHP!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.