Jump to content

Recommended Posts

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";

[code]

<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>
[/code]

Thank you for your time.
Link to comment
https://forums.phpfreaks.com/topic/8136-showing-username/
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/8136-showing-username/#findComment-29661
Share on other sites

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.


[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>[/code]

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??
Link to comment
https://forums.phpfreaks.com/topic/8136-showing-username/#findComment-30358
Share on other sites

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...
[code]
<!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>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/8136-showing-username/#findComment-30799
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/8136-showing-username/#findComment-31076
Share on other sites

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.