SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Profile problem

    Hi Guys,

    i am building a small basic site just to demonstrate what im learning from PHP/MYSQL i'm doing ok so far but have a problem i have a view members page, the basic code from the database is:

    Code:
             $profile = "SELECT * FROM users;";
             $profile_query = mysql_query($profile) or die(mysql_error());
    
             while($profile_array = mysql_fetch_array($profile_query))
             {
             echo "<a href='view_members.php?id=$profile_array[user_id]'>$profile_array[username]";
             echo "<br />";
    This makes the users name clickable, but what i was wanting to do is click the users name and be taken to they're profile page where all they're login information will be displayed, i'm not sure what to do from here lol

    any help would be appreciated

    Graham

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    hi Graham and welcome to the forums

    What you need to do is to set up a page called profile.php and using the information passed from the URL run a query using a WHERE clause.

    Something like:

    PHP Code:
    $incoming_id $_GET['id'];
    $sql mysql_query("select * from users where id = '"mysql_real_escape_string($incoming_id). "'"); 
    This will give you the record from the database corresponding to the ID passed from the link.

    HTH

    Spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by graham23s View Post
    Hi Guys,

    i am building a small basic site just to demonstrate what im learning from PHP/MYSQL i'm doing ok so far but have a problem i have a view members page, the basic code from the database is:

    Code:
             $profile = "SELECT * FROM users;";
             $profile_query = mysql_query($profile) or die(mysql_error());
    
             while($profile_array = mysql_fetch_array($profile_query))
             {
             echo "<a href='view_members.php?id=$profile_array[user_id]'>$profile_array[username]";
             echo "<br />";
    This makes the users name clickable, but what i was wanting to do is click the users name and be taken to they're profile page where all they're login information will be displayed, i'm not sure what to do from here lol

    any help would be appreciated

    Graham
    On your Query you need to select the pk (assuming it is user_id), then on your view_members.php you need to do something like this:
    PHP Code:
    if(isset ($_GET['$user_id']))
    {
     
     
    $user_id $_GET['user_id'];

     
    $query   "SELECT user_id, bla, bla FROM bla WHERE user_id = '$user_id'";


    hth

  4. #4
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    THANKS A LOT GUYS, that worked perfectly.

    i am now on the users profile page i modified the query to:

    $query = "SELECT user_id,username,email,country FROM users WHERE user_id = '$user_id'";

    what would be the best way to echo (that particular users) info out, would it be:

    echo 'user_id = $user_id';
    echo 'Username = $username';
    echo 'Email = $email';
    echo 'Country = $country';

    Thanks a lot for your help guys

    Graham

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Well that query should only bring out 1 result so assign a results handler to it
    PHP Code:

    $rows 
    mysql_fetch_assoc($sql);

    echo 
    $rows['id'];
    echo 
    $rows['whatever_field_name'];
    // etc 
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi spikeZ

    I appreciate all your input guys this is the code i ahve on the profile.php

    Code:
             <?php
             // Grab the users profile page details...
             $incoming_id = $_GET['user_id']; 
             $sql = mysql_query("select * from users where id = '". mysql_real_escape_string($incoming_id). "'"); 
             
             $rows = mysql_fetch_assoc($sql); 
             echo $rows['user_id']; 
             echo $rows['username']; 
             echo $rows['email']; 
             echo $rows['country']; 
             ?>
    but i get an error,Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in

    i know for a fact i haven't put in the correct code but i'm damned if i can see what it is lol

    once i get this error done im good honest lol

    thanks a lot guys

    Graham

  7. #7
    SitePoint Evangelist superuser2's Avatar
    Join Date
    Aug 2006
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you called the script where $_GET['user_id'] is a valid userid that actually exists? Otherwise, let's check for mysql errors. Right after

    PHP Code:
    $sql mysql_query("select * from users where id = '"mysql_real_escape_string($incoming_id). "'"); 
    put

    PHP Code:
    if(!$sql) { die("Mysql Error: " mysql_error()); } 
    Run the script and tell us what happens.

  8. #8
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Mate,

    i put the code in right after and it said:

    "Mysql Error: Unknown column 'id' in 'where clause'"

    i found the problem i just had to add the word: user in front of the id to make user_id now the error is away but doesn't display that particular users information the complete code is:

    Code:
             <?php
             // Grab the users profile page details...
             $incoming_id = $_GET['user_id']; 
             $sql = mysql_query("select * from users where user_id = '". mysql_real_escape_string($incoming_id). "'");       
             $rows = mysql_fetch_assoc($sql); 
             echo $rows['user_id']; 
             echo $rows['username']; 
             echo $rows['email']; 
             echo $rows['country']; 
             ?>
    although the info isn't displayed there isn't actually any errors to speak of lol

    cheers for any help

    Graham

  9. #9
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    See if there are any rows returned....
    PHP Code:
            <?php
             
    // Grab the users profile page details...
             
    $incoming_id $_GET['user_id']; 
             
    $sql mysql_query("select * from users where user_id = '"mysql_real_escape_string($incoming_id). "'");  
    // check for number of rows returned
    if(mysql_num_rows($sql) == 0) {
    echo 
    'No rows found';
    } else {     
             
    $rows mysql_fetch_assoc($sql); 
             echo 
    $rows['user_id']; 
             echo 
    $rows['username']; 
             echo 
    $rows['email']; 
             echo 
    $rows['country']; 
    }
             
    ?>
    Also check that your fieldnames from your database match what is in the $rows['field']
    ie:
    there is a field called username
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  10. #10
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Mate,

    Thanks for your help mate, i inputted the code it's coming back "No rows found", i have checked the database table names match up they do ok i am stumped , something dead simple i bet lol, is there anything else i could try?

    thanks for your time mate

    Graham

  11. #11
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Just looking at the first post and noticed that the link says:
    Code:
     echo "<a href='view_members.php?id=$profile_array[user_id]'>$profile_array[username]";
    Apart from the fact there is no closing </a> on it the variable is passed named id NOT user_id. Therefore.....
    PHP Code:
    $incoming_id $_GET['user_id']; 
    should be:

    PHP Code:
    $incoming_id $_GET['id']; 
    Try that
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  12. #12
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    WOOHOO

    That worked a treat mate, i can't thank you enough i am 1 bit further ahead in PHP now lol

    Thanks again mate

    Graham

  13. #13
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    np Graham, glad you got it sorted
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •