0

I want to display the particular username after login successful. by using session I am calling my username. But it is displaying all user names.

Please give suggestion to display only particular username. Here is my code.

In session :

$_SESSION ['admin_name'] = $row['name'];
$admin_name = $_SESSION['admin_name'];

Inserting in to DB :

 $sql = "INSERT INTO account_info (name)
         VALUES ( '$admin_name')";

Displaying :

<?php
    $count = 1;
    $sel_query = "Select * from account_info ORDER BY id;";
    $result = mysql_query($sel_query);
    while ($row = mysql_fetch_assoc($result)) { 
?>
        <td align="left"><?php echo $row["name"]; ?></td>
<?php 
        $count++; 
    }
?>

I think while displaying I need to filter name. Please reply to me anybody knows how to filter.

Update:

enter image description here

devpro
  • 16,184
  • 3
  • 27
  • 38
Keerthi
  • 13
  • 7

4 Answers4

0

You should re-approach this as it's poor design, but to get what you want to achieve:

while($row = mysql_fetch_assoc($result)) {
    if($row['name'] == $_SESSION['admin_name']) {
        ...
    }
}

Or even better...

$sel_query="Select name from account_info WHERE name = '$admin_name'";

Jack hardcastle
  • 2,748
  • 4
  • 22
  • 40
0
$sel_query="Select * from account_info WHERE name = '$admin_name' ";
Twinfriends
  • 1,972
  • 1
  • 14
  • 34
0

You don't need to save session values into database to show it after login successful.

Only store it into session array like this:

$_SESSION['user_name'] = $row['name'];

and in login page check this session array is set or not.If set then display it like:

if(isset($_SESSION['user_name'])) { echo $_SESSION['user_name']; }
Abasaheb Gaware
  • 509
  • 4
  • 13
-1

You are getting the all username because of this query:

Select * from account_info ORDER BY id;

This will return all user data, you need to add username in WHERE clause for particular user as:

Select * from account_info WHERE name = '$admin_name';

More important, i didn't see session_start() function in your code, don't forgot to use this, otherwise you can't get the $_SESSION values.

Other important thing, your code is open for SQL Injection, you can use Prepared Statement for preventing SQL Attack. This will help you: How can I prevent SQL injection in PHP?

Very Special point, mysql_* is deprecated and closed in PHP 7.

Side Notes:

In your current query, no need to use ORDER BY id because when you use WHERE for particular user this part should be useless here.

Don't know, where are you using $count++; in your requirement, you need to print only particular user data so this is also useless here.

Community
  • 1
  • 1
devpro
  • 16,184
  • 3
  • 27
  • 38