/    Sign up×
Community /Pin to ProfileBookmark

I cannot get my page to display user information after they logged in. Help, please?

I am new to PHP/MYSQL. The database works totally fine. I have a username, password, first, and last name field in my database currently. The log in works perfectly fine. I have all the statements needed and whatnot and my code validates whether or not I was able to log in. Here’s my predicament…

My form uses the POST method to retrieve the username/password and blah blah blah. I can have it display the username on any other pages by using the session cookies and whatnot. I just need to know how to access my first and last name information.

For example. When the user logs in and they go to the next page, I want it to say: Welcome, Joe Blah. The text, Joe Blah, isn’t inputted into the form or submitted any way. It’s located IN the database. I just don’t know how to access it…

Once again, I’m sorry if some of the terms I used were incorrect. I actually JUST learned how to do this from a good friend of mine who is a web developer. I just need to know a little bit more. I’m catching on pretty well…I just can’t seem to find what I’m looking for. I’ve browsed the internet an hour or so prior to this, and everything that has to do with displaying the user’s info has to do with the POST method and they just re-display it on a different page.

Well, thanks again!

  • Tim
  • to post a comment
    PHP

    9 Comments(s)

    Copy linkTweet thisAlerts:
    @NogDogDec 02.2009 — When you do the login, you can add any columns you want to the query you use to validate the login. If it's successful, store that data in the $_SESSION array with whatever else you are already storing to indicate that the user is logged in.
    Copy linkTweet thisAlerts:
    @timlabahnauthorDec 02.2009 — Ah, I see. Logically, I thought that would be a good idea; however, I'm not sure what I'd exactly write..? Like, the proper syntax and whatnot. I'm still browsing and finding coding examples. Bare with me...I'm learning quick for only being into this for less than 12 hours.
    Copy linkTweet thisAlerts:
    @NogDogDec 02.2009 — Can you show us the code you're using now (within [noparse][code=php]...[/code][/noparse] tags) to do the login processing?
    Copy linkTweet thisAlerts:
    @speedy78Sep 26.2011 — I am working on the same thing currently. And the problem I am having is figuring out how to get it where my page displays only the user information that is logged in. I'll locate and post my login.php and main.php on my computer shortly and display the code I currently have and a link to the site so that you can view it.
    Copy linkTweet thisAlerts:
    @speedy78Sep 26.2011 — My login.php
    [code=php]
    <?php
    require("db/conn.php");
    session_start();
    $uname = $_POST['uname'];
    $pword = $_POST['pword'];
    if($uname&&$pword)
    {
    $query = mysql_query("SELECT * FROM users Where uname='$uname'");
    $numrows = mysql_num_rows($query);
    if($numrows!=0)
    {
    while ($row =mysql_fetch_assoc($query))
    {
    $dbuname = $row['uname'];
    $dbpass = $row['pword'];
    }
    if($uname==$dbuname&&md5($pword)==$dbpass)
    {
    echo"<script type='text/javascript'>
    <!--
    window.location = 'main.php'
    //-->
    </script>";
    $_SESSION['uname']=$uname;
    }
    else
    echo "Incorrect Password";

    }
    else
    die("That account doesn't exist.");
    }
    else
    die("Please enter <b>email</b> and <b>password</b>!");

    ?>
    [/code]
    Copy linkTweet thisAlerts:
    @speedy78Sep 26.2011 — My main.php file it directs to once the user is loggedin

    [code=php]
    <?php
    require("db/conn.php");
    $sql = "select * from users";
    $result = mysql_query ($sql);

    while ($row = mysql_fetch_array($result))
    {
    $field1= $row["fname"];
    $field2= $row["lname"];
    $field3= $row["add1"];
    $field4= $row["city"];
    $field5= $row["state"];
    $field6= $row["zip"];
    $field7= $row["country"];
    $field8 = $row["mail"];

    echo "
    <table>
    <tr>
    <td>
    $field1 $field2
    </td>
    </tr>
    <tr>
    <td>
    $field3
    </td>
    </tr>
    <tr>
    <td>
    $field4,
    $field5
    $field6
    </td>
    <tr>
    <tr>
    <td>
    $field7
    </td>
    </tr>
    <tr>
    <td>
    $field8
    </td>
    </tr>
    </table>
    <br>
    <table>
    <tr>
    <td>
    <img src='img/user.png' border='0' width='16px' height='16px'>
    </td>
    <td>
    <font color='#2554C7' face='New Times Roman' size='2'>
    <a href='details.php' border='0'>Update your details</a>
    </font>
    </td>
    <td>
    <img src='img/dollar.png' border='0' width='16px' height='16px'>
    </td>
    <td>
    <font color='#2554C7' face='New Times Roman' size='2'>
    <a href='#' border='0'>Add funds</a>
    </font>
    </td>
    </tr>
    </table>
    ";
    }
    ?>
    [/code]
    Copy linkTweet thisAlerts:
    @speedy78Sep 27.2011 — I chnaged my main php to this
    [code=php]
    <?php
    require("db/conn.php");
    $id = $_SESSION['ID']; //Get user's ID
    $result = mysql_query("SELECT * FROM users where id='$id'");
    $row = mysql_fetch_assoc($result);

    echo "Your user name is: <br>";
    echo $row['fname'];
    echo " and you registered at: <br>";
    echo $row['date'];


    ?>
    [/code]


    Image

    http://www.hellfire-angels.us/examp/example.png
    Copy linkTweet thisAlerts:
    @speedy78Sep 27.2011 — [code=php]
    <?php
    require ("db/conn.php");
    session_start();
    $user = $_SESSION['uname'];
    if($user)
    {
    $id = $row['id'];
    $sql = mysql_query( "SELECT * FROM users" );
    $row = mysql_fetch_assoc($sql);
    echo "<tr>
    <td valign='top'>
    <font size='2' face='New Times Roman'><strong>First Name:</strong> ".$row['fname']."</font><br>
    <font size='2' face='New Times Roman'><strong>Last Name:</strong> ".$row['lname']."</font><br>
    <font size='2' face='New Times Roman'><strong>Username:</strong> ".$row['uname']." </font><br>
    <font size='2' face='New Times Roman'><strong>E-mail:</strong> ".$row['mail']."</font><br>
    </td>
    </tr>";
    }
    else
    die("");
    ?>
    [/code]


    The problem I am having now is that it only displays one user and that's with id of id 1 doesn't show if your id #2 shows ID 1's info not ID 2
    Copy linkTweet thisAlerts:
    @speedy78Sep 27.2011 — Fixed main.php
    [code=php]
    <?php
    require ("db/conn.php");
    session_start();
    $user = $_SESSION['uname'];
    if($user)
    {
    $id = $row['id'];
    $sql = mysql_query( "SELECT * FROM users WHERE uname='$user'" );
    $row = mysql_fetch_assoc($sql);
    echo "<tr>
    <td valign='top'>
    <font size='2' face='New Times Roman'><strong>First Name:</strong> ".$row['fname']."</font><br>
    <font size='2' face='New Times Roman'><strong>Last Name:</strong> ".$row['lname']."</font><br>
    <font size='2' face='New Times Roman'><strong>Username:</strong> ".$row['uname']." </font><br>
    <font size='2' face='New Times Roman'><strong>E-mail:</strong> ".$row['mail']."</font><br>
    </td>
    </tr>";
    }
    else
    die("");
    ?>
    [/code]
    ×

    Success!

    Help @timlabahn spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.8,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...