php - i am Unable to fetch the user id of logged in user from my data base
I am using PHP to get the user id to display the profile details of logged in user but i am unable to fetch the id Here is my code in login page
$email = mysqli_real_escape_string($con,$_POST['email']);
$q = "select * from users where email = '$email'";
$rows=mysql_fetch_assoc($q);
$id = $rows['$id'];
$result = mysqli_query($con, $q);
$num = mysqli_num_rows($result);
if($num == 1){
$_SESSION['user'] = $email;
header("location:home.php?id=$id");
}else{
$reg = "insert into users(email) values ('$email')";
mysqli_query($con, $reg);
header('location:index.php?msg');
}
Here is my home page code
<?php
session_start();
if(!isset($_SESSION['user'])) {
header("Location:index.php");
exit;
}
$id = $_REQUEST['id'];
include_once('conn.php');
$query="select * from users WHERE id= '$id' ";
$result=mysql_query($query);
?>
Answer
Solution:
Looks like you appended the $id
from your login page to home.php?id=$id
and then in your home.php file, you are trying to fetch that $_GET['id'] value.
Here are the things to help you debug.
- First check if from your loginpage that upon successful login you are redirected to home.php?id=$id. Then check the URL if you have correct id
- Use $_GET['id'] instead. You can try to
echo $_GET['id'];
to see if there's any value.