Display Only the Current User'S Details Using PHP and MySQL

PHP Displaying user info when logged in

$strSQL = "SELECT * FROM users";

Why that query? if you say you wanted to display only the info about users logged in, you are getting all users without conditions

Do the query for the user who is logged in at the moment, something like

$strSQL = "SELECT * FROM users WHERE username = '".$_SESSION['username']."'";

or somethinbg like this

  <?php

session_start(); //Add this

//Also you have to add your connection file before your query
require('db.php');

// SQL query
$strSQL = "SELECT username, email FROM users WHERE user_id = '".$_SESSION['user_id']."'";

// Execute the query (the recordset $rs contains the result)
$rs = mysqli_query($myConnection, $strSQL);

// Loop the recordset $rs
// Each row will be made into an array ($row) using mysqli_fetch_array
while($row = mysqli_fetch_array($rs)) {

// Write the value of the column FirstName (which is now in the array $row)
echo $row['username'] . "<br />";
echo $row['email'] . "<br />";

}

// Close the database connection
mysqli_close($myConnection);

?>

I think it should have to work, tell me if it worked for you

How to display current user details from database in separate page in php

<?php
include 'config.php';
error_reporting(E_ERROR);
session_start();
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$email=$_POST['email'];
$pass=$_POST['pass'];
$phone=$_POST['phone'];
$sex_select=$_POST['sex_select'];
$month=$_POST['month'];
$day=$_POST['day'];
$year=$_POST['year'];

$result = mysql_query("INSERT INTO crop(fname, lname, email, pass, phone,`sex_select`, month,day,year)
VALUES ('$fname', '$lname', '$email', '$pass','$phone','$sex_select', '$month','$day','$year')");
if(mysql_affected_rows()>1)
{

//it will redeirect you to. your any file.
//you can give any file name here.
$_SESSION['fname']=$fname;//you are adding value to the session
$_SESSION['lanme']=$lname; //you are adding value to the session
//Do this for rest of your value
header('Location:Your_File_name.php');
}



if (!$result) {
die(msg(0,"wrong query"));
}
?>

Show specific content based on current user session PHP

If logged in user shouldn't edit other user's posts, then don't show the edit column, then you can do simple if check for the column Edit like below

    while ($row_d = mysql_fetch_assoc($check_d)) {
echo "<h1>Post made by: ".$row_d['username']."</h1>";
$check_u = mysql_query("SELECT * FROM topics WHERE topic_creator='".$row_d['username']."'");
while ($row_u = mysql_fetch_assoc($check_u)) {
$id = $row_u['topic_id'];
echo "<tr>";
echo "<td>".$row_u['topic_id']."</td>";
echo "<td><a href='topic.php?id=$id'>".$row_u['topic_name']."<br /></a></td>";
echo "<td>".$row_u['topic_creator']."<br /></td>";
echo "<td>".$row_u['date']."<br /></td>";
// Add if condition here
if($_SESSION['current_logged_in_user_id'] === $row_u['topic_creator_id']) {
echo "<td><a href='edit.php?edit=$id'>Edit</a><br /></td>";
}
echo "</tr>";
}
}

but don't use mysql_* functions. use mysqli or PDOs for security reasons like protecting yourself from sql injection attacks.



Related Topics



Leave a reply



Submit