User welcome message in php

Posted by user225269 on Stack Overflow See other posts from Stack Overflow or by user225269
Published on 2010-04-12T06:42:11Z Indexed on 2010/04/12 6:43 UTC
Read the original article Hit count: 212

Filed under:
|

How do I create a user welcome message in php. So that the user who has been logged on will be able to see his username. I have this code, but it doesn't seem to work.

    <?php
$con = mysql_connect("localhost","root","nitoryolai123$%^");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("school", $con);





$result = mysql_query("SELECT * FROM users
WHERE Username='$username'");

while($row = mysql_fetch_array($result))
  {
  echo $row['Username'];
  echo "<br />";
  }
?>

I'm trying to make use of the data that is inputted in this login form:

<form name="form1" method="post" action="verifylogin.php">
<td>
<table  border="0" cellpadding="3" cellspacing="1" bgcolor="">
<tr>

<td  colspan="16" height="25"  style="background:#5C915C; color:white; border:white 1px solid; text-align: left"><strong><font size="2">Login User</strong></td>
</tr>
<tr>

<td width="30" height="35"><font size="2">Username:</td>
<td width="30"><input  name="myusername" type="text" id="idnum" maxlength="5"></td>
</tr>

<tr>

<td width="30" height="35" ><font size="2">Password:</td>
<td width="30"><input name="mypassword" type="password" id="lname" maxlength="15"></td>


</tr>

<td align="right" width="30"><td align="right" width="30"><input type="submit" name="Submit" value="Submit" /></td> <td align="right" width="30"><input type="reset" name="Reset" value="Reset"></td></td>
</tr>
</form>

But this, verifylogin.php, seems to be in the way.

<?php
$host="localhost";
$username="root"; 
$password="nitoryolai123$%^"; 
$db_name="school"; 
$tbl_name="users"; 


mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");


$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword'];


$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);


$count=mysql_num_rows($result);


if($count==1){

session_register("myusername");
session_register("mypassword"); 
header("location:userpage.php");
}
else {
echo "Wrong Username or Password";
}
?>

How do I do it? I always get this error when I run it:

Notice: Undefined variable: username in C:\wamp\www\exp\userpage.php on line 53

Can you recommend of an easier on how I can achieve the same thing?

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql