Reputation: 163
this is my validation page
<html>
<body>
<?php
function error_found(){
header("Location: login.php");
}
set_error_handler('error_found');
?>
<?php
$con=mysqli_connect('localhost','root','','mydb');
session_start();
if(isset($_POST['loginbtn'])){
$email=$_POST['email'];
$password=$_POST['pwd'];
$result=mysqli_query($con,'select * from myguests where email="'.$email.'" and password="'.$password.'"');
if(mysqli_num_rows($result)==1)
{
$_SESSION['email']= $email;
header("Location: welcome.php");
}
else
echo"INVALID ACCOUNT";
}
?>
</body>
</html>
this is my user profile page
<html>
<body>
<?php
require_once 'valid.php';
session_start();
echo"welcome " .$_SESSION['email'];
$res=mysqli_query($conn,"SELECT * FROM MyGuests WHERE email=".$_SESSION['email']);
$userRow=mysqli_fetch_array($res);
echo $userRow;
?>
<br><a href="logout.php">logout</a>
</body>
</html>
>
I am not able to fetch the data from the database it shows me the error.I think I made mistake in declaring a session variable to the SQL query
Upvotes: 0
Views: 696
Reputation: 2372
You have an error in commented line check.
$res=mysqli_query($conn,"SELECT * FROM MyGuests WHERE email='".$_SESSION['email']."'"); // this line has an issue with "'"
<html>
<body>
<?php
require_once 'valid.php';
session_start();
echo"welcome " .$_SESSION['email'];
$res=mysqli_query($conn,"SELECT * FROM MyGuests WHERE email='".$_SESSION['email']."'"); // Missing "'" in query to enclosed parameter.
$userRow=mysqli_fetch_array($res);
echo $userRow;
?>
<br><a href="logout.php">logout</a>
</body>
</html>
Upvotes: 0
Reputation: 872
You have an error on your sql.
$res=mysqli_query($conn,"SELECT * FROM MyGuests WHERE email=".$_SESSION['email']);
It should be:
$res=mysqli_query($conn,"SELECT * FROM MyGuests WHERE email='".$_SESSION['email']."');
Upvotes: 2