1

i'm making a small account using login, now i have to display name from ID who is logged in. suppose, i have two accounts into my database now as i print his name, it shows ID no. 1's name while i'm logged in using ID no. 2 can you tell me what's going on? where i'm suppose to be wrong?,

here is my change_setting_db.php :

<?php
$con=mysqli_connect("localhost","root","Bhawanku","members");
// Check connection

if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM admin");
?>

and here is my general_setting.php :

<div id="change_name">
    <label><strong>Name: </strong></label>
        <?php
        include('change_setting_db.php');

        while($row = mysqli_fetch_array($result))
        {
            echo $row['first_name']." ".$row['last_name'];
        }
        ?>
        <a id="display_float" href="change_name.php">Edit</a>
    </div><hr>

EDITED

i tried but it's not working..

<?php
$con=mysqli_connect("localhost","root","Bhawanku","members");
// Check connection

if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM admin");
if ($row = mysqli_fetch_array($result)) {
    $id=$row['id'];
mysqli_query($con,"SELECT * FROM admin WHERE id='$id' ");
}
?>

6 Answers 6

1

When you make the query to the database in $result = mysqli_query($con,"SELECT * FROM admin"); you need to pass the user id stored in a session variable or something.

Look at this:

$uid = $_SESSION['uid'];
$result = mysqli_query($con, "SELECT * FROM admin WHERE uid = '$uid'");
Sign up to request clarification or add additional context in comments.

1 Comment

i tried your code but it's showing this error: Undefined index: id in C:\Users\Raj\PhpstormProjects\new linkvessel\change_setting_db.php on line 10
0

Why would you expect this code to show the logged in user, given that you are doing:

SELECT * FROM admin

This retrieves all rows from the table with no condition. You need to add a WHERE clause such as:

SELECT * FROM admin WHERE user_id = ?

Typically the logged in user's user_id would be stored in the session. Also remember to use a prepared statement instead of concatenating the user_id directly into the query.

1 Comment

How to display id using row??
0

your sql query shoulb be

SELECT * FROM admin WHERE id='1';

Comments

0
SELECT * FROM admin WHERE username = $username AND password = $password);

Row ID:<?=  $row['id'] ?>

2 Comments

and how to pass $username and $password please help :(
In your login form, username field has name='username' and password field has name='password' and on the page that receives the form submission has $username = $_POST['username']; and the same for password
0

Here pass the "ID" of user in where condition for query if you want particular users name.

$con=mysqli_connect("localhost","root","Bhawanku","members");
//Check connection

if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM admin where id=(pass your id's value here)");
?>

or

  1. you can just keep the id in session if the login credentials are correct.
  2. and when you have id in your hand just fire a select query with where condition like

    $result = mysqli_query($con,"select * from admin where id='."$id".'"); if(count($result>0) { // now display the name of that user echo "Id = ".$result[0]['id']; echo "Name = ".$result[0]['first_name']." ".$result[0]['first_name']; } else { // handle the error condition here echo "no result found"; }

2 Comments

You are accessing all columns from database table with particular id so you will get all data for that like(ID,FIRST_NAME,LAST_NAME)as per your code.
see my edited it's not working and as you said i did all but it's not working
0

I think this should help you out,

<!DOCTYPE html>
<html>
<head>
</head>
<body id=""body>
    <div id="body-container">
        <form action="index.php" method="POST">
            <input type="text" id="username" name="username" value="" autocomplete="false" spellcheck="false" placeholder="Username" />
            <input type="password" id="password" name="password" value="" autocomplete="false" spellcheck="false" placeholder="Password" />
            <input type="submit" id="submit" name="submit" value="submit" />
        </form>
    </div>

    <?php
        $username = $_POST['username'];
        $password = $_POST['password'];

        $database = mysqli_connect("database-ip", "database-username", "database-password", "database-name", "database-port"); //fill in the examples correctly!

        $fetch_username = mysqli_query($database, "SELECT * FROM login WHERE username='$username'");

        $fetch_password = mysqli_query($database, "SELECT * FROM login WHERE password='$password'");

        $check_username = mysqli_fetch_array($fetch_username);
        $check_password = mysqli_fetch_array($fetch_password);

        if($username = $check_username && $password = $check_password){
            echo "loged in";
        }

        if($username != $check_username || $password != $check_password){
            echo "incorrect username or password"; 
        }
    ?>


</body>
</html>

i really recommend you check out the official php documentation at:

http://www.php.net/manual/en/

i hoped this helped you out (=

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.