displaying the user's name

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

mcookie82
New php-forum User
New php-forum User
Posts: 9
Joined: Thu Mar 20, 2003 7:14 am

displaying the user's name

Postby mcookie82 » Wed Mar 26, 2003 1:06 pm

hey, me again,

i wrote a topic a while back asking about log ins, but ive totally changed the whole code (for the better i hope). I'm still having the same problem though, i think im having problem writing the sql code to get the query result i want. So heres the whole situation again.

As i stated before i have two primary tables at the moment. A cusatomers table and an authorisation table which look like as follows;

customers
+------------+----------------+-------------------+---------
| customerid | name | address | city
+------------+----------------+-------------------+---------
| 1 | Travis Perkins | 12 Trinity Avenue | Notts
+------------+----------------+-------------------+---------

auth
+----------+-------+------------+
| username | pass | customerid |
+----------+-------+------------+
| testuser | pass | 0 |
| travisp | pass1 | 1 |
+----------+-------+------------+

and the page code is as follows;

Code: Select all

<?

   require_once("db_function.php");
   require_once("html_function.php");
   session_start();
   
   if ($userid && $password)
   {
        // if the user has just tried to log in
     
        $conn = db_connect();
      if (!$conn)
         return "Could not connect to database server - please try again later.";
      
        $query = "select * from auth "
              ."where username='$userid' "
              ." and pass='$password'";
        $result = mysql_query($query);
        if (mysql_num_rows($result) >0 )
        {
          // if they are in the database register the user id
          $valid_user = $userid;
          session_register("valid_user");
        }   
   }
   
   // get html header
   do_html_header("Home Page");
   
     if (session_is_registered("valid_user"))
     {
       echo "You are logged in as: <b>$valid_user</b> <br>";
       echo "<br><a href=\"logout.php\" title=\"Click here to log out\">Log out</a><br>";
     }
     else
     {
       if (isset($userid))
       {
            // if they've tried and failed to log in
            echo "Could not log you in";
       }
       else
       {
            // they have not tried to log in yet or have logged out
            echo "You are not logged in.<br>";
       }

       // provide form to log in
       echo "<form method=post action=\"loginsession.php\">";
       echo "<table>";
       echo "<tr><td>Userid:</td>";
       echo "<td><input type=text name=userid></td></tr>";
       echo "<tr><td>Password:</td>";
       echo "<td><input type=password name=password></td></tr>";
       echo "<tr><td colspan=2 align=center>";
       echo "<input type=submit value=\"Log in\"></td></tr>";
       echo "</table></form>";
   
     }
   
   echo "<br><a href=\"members_only.php\" title=\"Click here to get to the members section\">Members section</a>";
   
   // get html footer
   do_html_footer();
   
?>



The only query that i got, allows me to display the username of the customer, which in this case would be 'travisp'.

But what i want it to do, is to take the customerid from the same row as the username and the password that were entered, and reference it into the customers table, and then take the name value from that table and display it, rather than saying

"You are logged in as: travisp"
i want it to say
"You are logged in as: Travis Perkins"

Is it important to keep the username as the session variable, or should it be changed to the user id, but would this complicate the code more.

Please, please, please, please, please help! I can seem to create queries that can find product information for my search page, but doing somethig like this seems to be beyond me.

Cheers, Mark[/code]

TheIceman5
New php-forum User
New php-forum User
Posts: 28
Joined: Tue Jan 21, 2003 9:31 pm
Contact:

Postby TheIceman5 » Sat Mar 29, 2003 5:13 pm

looks like you need to do an inner join query to replace this query.

$query = "select * from auth "
."where username='$userid' "
." and pass='$password'";
$result = mysql_query($query);
if (mysql_num_rows($result) >0 )
{
// if they are in the database register the user id
$valid_user = $userid;
session_register("valid_user");
}
}

it will then give you the results of both tables and you can combine the first name and last name.

mcookie82
New php-forum User
New php-forum User
Posts: 9
Joined: Thu Mar 20, 2003 7:14 am

ok

Postby mcookie82 » Sun Mar 30, 2003 12:59 pm

cheers, i'll try and do something along those lines, but i fear that i may be asking for help very soon. And if others wanna help, please do so :lol: i'm open to as much input as anyone wants to give :P

cheers

Markie

mcookie82
New php-forum User
New php-forum User
Posts: 9
Joined: Thu Mar 20, 2003 7:14 am

got itworking, now however ....

Postby mcookie82 » Tue Apr 01, 2003 12:23 pm

using the following code i was able to get the page towork, so that it says
Travis Perkins
and not
travisp
.

Code: Select all

<?

   require_once("db_function.php");
   require_once("html_function.php");
   session_start();
   
   if ($userid && $password)
   {
        // if the user has just tried to log in
     
        $conn = db_connect();
      if (!$conn)
         return "Could not connect to database server - please try again later.";
      
        $query = "select * from auth as a, customers as c"
              ." where a.username='$userid'"
              ." and a.pass='$password'"
         ." and a.customerid=c.customerid";
        $result = mysql_query($query);
        if (mysql_num_rows($result))
        {
          // if they are in the database register the user id
          $valid_user = $userid;
          session_register("valid_user");
        }   
   }
   
   // get html header
   do_html_header("Home Page");
   
     if (session_is_registered("valid_user"))
     {
      $row = mysql_fetch_array($result);
       echo "You are logged in as: <b>";
      echo stripslashes ($row['name']);
       echo "</b><br><br><a href=\"logout.php\" title=\"Click here to log out\">Log out</a><br>";
     }
     else
     {
       if (isset($userid))
       {
            // if they've tried and failed to log in
            echo "Could not log you in";
       }
       else
       {
            // they have not tried to log in yet or have logged out
            echo "You are not logged in.<br>";
       }

       // provide form to log in
       echo "<form method=post action=\"loginsession2.php\">";
       echo "<table bgcolor=#cccccc>";
       echo "<tr><td>Userid:</td>";
       echo "<td><input type=text name=userid></td></tr>";
       echo "<tr><td>Password:</td>";
       echo "<td><input type=password name=password></td></tr>";
       echo "<tr><td colspan=2 align=center>";
       echo "<input type=submit value=\"Log in\"></td></tr>";
       echo "</table></form>";
   
     }
   
   echo "<br><a href=\"members_only.php\" title=\"Click here to get to the members section\">Members section</a>";
   
   // get html footer
   do_html_footer();
   
?>


it brings all the info i need on the user by using this query by referencing the customerid on both tables. I then take the name element out of the result array to get Travis Perkins.

However i wish to carry this line of information to the members page. I used the following code to try and keep the
You are logged in as Travis Perkins
line;

Code: Select all

<?
     
   require_once("html_function.php");
     session_start();
   
   // get html header
   do_html_header("Members Page");

     [b]// check session variable
     if (session_is_registered("valid_user"))
     {
       $row = mysql_fetch_array($result);
       echo "You are logged in as: <b>";
      echo stripslashes ($row['name']);[/b]
       echo "<p>What would you like to do?</p>";
      echo "<ul>";
      echo "<li><a href=\"search.php\" title=\"Click here to search for products\">Search</a></li>";
      echo "</ul>";
     }
     else
     {
       echo "<p>You are not logged in.</p>";
       echo "<p>Only logged in members may see this page.</p>";
     }

     echo "<a href=\"loginsession.php\" title=\"Click here to get back to the main page\">Back to main page</a>";
   
   // get html footer
   do_html_footer();
   
?>


i believe the problem is with the bold bit with the whole session variable thingy. It doesnt output the same as in the log in page. This time i get an error message telling me that it was some sort of inaccurate SQL request, when trying to request the 'name' element again on this page.

Is there any way to get Travis Perkins to appear again without having to input the whole query code that i used in the log in page??????

thanks

cookie[/b]


Return to “PHP coding => General”

Who is online

Users browsing this forum: Google [Bot] and 1 guest