Jump to content


Photo

Showing username


  • Please log in to reply
6 replies to this topic

#1 Hooch

Hooch
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 22 April 2006 - 09:35 PM

Hello all.
I am wanting to have my header say welcome "username hare" when someone is on my site.
I have followed a tut on making a database with log in and out functions. Both work too.
Thing is, I can't get the username to show up.
Could someone be kind enough to have a look?
Here is the line I would like the name entered.
echo "$username";


<table width="700" height="100" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#00FF00">
    <tr>
        <td height="81" colspan="4" background="images/header-700-81.jpg"> </td>
    </tr>
<tr>
          <td width="148" height="19">
          <div align="right">
            <?php
        if (!$_COOKIE['id']){
        echo "<a href=\"signup.html\">Register</a>";
        echo "  ";
        echo "<a href=\"login.html\">Log In</a>";
        die("");
        }
        echo "Welcome - ";
        ?>      
        </div></td>
  <td width="126" height="19">
        <?php
        // Connect to the database
        $server = "localhost";
        $dbuser = "username";
        $dbpass = "password";
        $dbname = "db_name";
        mysql_connect($server,$dbuser,$dbpass) or die ("connection error"); // make connection
        mysql_select_db($dbname); // select database
        // Get users information
        $result = mysql_query("SELECT * FROM `users` WHERE `id` = '".mysql_escape_string($u)."' ");
        while($r=mysql_fetch_array($result))
        {
        $username=$r["username"];
        echo " ";
        echo "$username";
        }
        ?>    
        </td>
       <td width="212" height="19"> </td>
        <td width="214"> </td>
    </tr>
</table>

Thank you for your time.

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 22 April 2006 - 10:05 PM

First, I would suggest that you log your user at the top of page in the PHP script block, and then simply echo the desired variable in the HTML code itself -- it's much cleaner this way.

Second, as for why it's not working, where is this $u variable coming from? Are you sure that your query returns any results, and executes sucessfully? I don't see any mysql_error() checks.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 Hooch

Hooch
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 25 April 2006 - 01:08 AM

Hi fenway. Thank you for your reply.
I am no coder..I was trying to mesh two tutorials.
But I did get things to work. Here's the code.


<table width="700"  height="91" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#00FF00">
<tr>
       <td height="81" colspan="3" background="images/header-700-81.jpg"> </td>
</tr>
<tr valign="bottom">
      <td width="416" height="10" bgcolor="#CCCCCC">
        <div align="left">
              <?php
            $msgbox = "";
             if (!$_COOKIE['id']){
            echo "<form action=\"login.php\" method=\"post\">name: <input type=\"text\" name=\"username\" size=\"8\"> password: <input type=\"password\" name=\"password\" size=\"8\"> <input type=\"image\" SRC=\"images/submit.jpg\" class=\"submit\" name=\"submit\"></form>"; 
            echo "<a href=\"signup.html\">Register</a>";
             }
            else
            {
             $msgbox .= "Welcome - ";
             // Connect to the database
             $server = "localhost";
             $dbuser = "user";
             $dbpass = "pass";
             $dbname = "db";
             mysql_connect($server,$dbuser,$dbpass) or die ("connection error"); // make connection
             mysql_select_db($dbname); // select database
             // Get users information
             $result = mysql_query("SELECT * FROM users WHERE id='".mysql_escape_string($_COOKIE['id'])."'");
             $r=mysql_fetch_assoc($result);
             $msgbox .= $r["username"];
            }
            echo $msgbox;
            ?>   
        </div>
    </td>
   
   <td width="151" height="19"> </td>

   <td width="133" height="19"> </td>
   
 </tr>
</table>

Everything seems to be working except the login and register.
It's all in the same table row. I made the box large enough for all to fit on one line,
but the register link is below the login fields when I upload it to the web.

Any ideas??

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 25 April 2006 - 05:45 PM

First, you need to close your FORM tag. Second, can you past the generated HTML code?
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 Hooch

Hooch
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 26 April 2006 - 01:55 AM

As far as I can tell the form is closed. But I'm sure I"m not following you.

For the generated do you mean view source?
If so...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>TIA</title>
<link rel="stylesheet" type="text/css" href="includes/tia.css" />
</head>
<body>

<table width="700"  height="91" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#00FF00">
<tr>
       <td height="81" colspan="3" background="images/header-700-81.jpg"> </td>
</tr>
<tr valign="bottom">
      <td width="416" height="10" bgcolor="#CCCCCC">
        <div align="left">
              <form action="login.php" method="post">name: <input type="text" name="username" size="8"> password: <input type="password" name="password" size="8"> <input type="image" class="submit" name="submit"></form><a href="signup.html">Register</a>   
        </div>
    </td>
   
   <td width="151" height="19"> </td>

   <td width="133" height="19"> </td>
   
 </tr>
</table><table width="700" border="2" align="center" cellpadding="0" cellspacing="0" bordercolor="#BE8647">
  <tr>
    <td width="161"> </td>
    <td width="296"> </td>
    <td width="233"> </td>
  </tr>
  <tr>
    <td>Test left </td>
    <td>Test middle </td>
    <td>Test right </td>
  </tr>
</table>


</body>
</html>


#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 26 April 2006 - 07:49 PM

Sorry, I missed to close form tag.... but forms are like tables, and have an implicit line break after them. That's why you'll never see them on one line. You can either a) put the link inside the form, or b) set the form's display attribute to "inline" via CSS.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#7 Hooch

Hooch
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 26 April 2006 - 11:54 PM

Thanks fenway.
I'm going to put them in thier own cell of the table then. This should fix it.

Now I know why they were offset like that.

Later...Hooch




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users