user6582683 user6582683 - 1 month ago 13
PHP Question

Display Full name instead of username in php

I have a login table containing

Name
,
Username
and
Password
. When user logins with username and password, I want to display the name of the user instead of the username. I want to display user's name in the next page as Welcome NAME instead of Welcome USERNAME.

Here is the HTML:

<form class="form-basic" method="post" action="login.php">
<div class="form-row">
<label>
<span>Username</span>
<input type="text" name="username" id="username" >
</label>
</div>

<div class="form-row">
<label>
<span>Password</span>
<input type="password" name="password" id="password" >
</label>
</div>

<div class="form-row">
<button type="submit" name="submit" id="submit"/>SUBMIT</button>
</div>

</form>


PHP code:



<?php

// your values are stored in cookies, then you can login without validate
include_once 'db.php';

// login validation in php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$username = mysql_real_escape_string(stripslashes($_POST['username']));
$password = $_POST['password'];

$query = mysql_query("select name,username,password from reglogin where username='".$username."' AND password='".$password."'");

$numrows = mysql_num_rows($query);

if ($numrows != 0) {
while ($row=mysql_fetch_assoc($query)) {
$dbname=$row['username'];
$dbpassword=$row['password'];
}

if($username == $dbname && $password == $dbpassword)
{
$_SESSION['sess_user']=$name;
echo "<script>window.open('registration1.php','_self')</script>";
}
}
else {
echo "<script>alert('Invalid! Username or Password')</script>";
}
}
?>

Answer

You can replace your code with this one: Your errors:

  1. In the line number 8 you have an Undefined Index called name.
  2. You are using that in value for $_SESSION which you can get from database while fetching the username and password.

Review your query what you are selecting and what you fetched. Changes I have made:

  1. check line number 8.
  2. check line number 20

Hope this will help you.

// your values are stored in cookies, then you can login without validate
include_once 'db.php';

// login validation in php
if (isset($_POST['submit'])) {
    // $name = $_POST['name'];   // this is not posted 
    $username = mysql_real_escape_string(stripslashes($_POST['username']));
    $password = $_POST['password'];

    $query = mysql_query("select name,username,password from reglogin where username='".$username."' AND password='".$password."'");

    $numrows = mysql_num_rows($query);

    if ($numrows != 0) {
        while ($row=mysql_fetch_assoc($query)) {
            $dbname=$row['username'];
            $dbpassword=$row['password'];
            $name =$row['name'];
        }

       if($username == $dbname &&  $password == $dbpassword)
       {
           $_SESSION['sess_user']=$name;
            echo "<script>window.open('registration1.php','_self')</script>";
        }   
    }
    else {
       echo "<script>alert('Invalid! Username or Password')</script>";
    }
}
?>