Paspartu
Paspartu

Reputation: 107

User name is not displayed

I'm trying to display username (cocname), but nothing happened. Do you see why? there is no error but the username is missing also and "Hello" is not displayed.Thank you.

<?php
session_start();
include_once 'config.php';
$prepend = "<span class='cocname'>";
$append = "</span>";
if (!isset($_SESSION['email'])) {
header("Location: signin.php");
}
$query = "SELECT cocname FROM users WHERE email=".$_SESSION['email'];
$result = $connect->query($query);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc())
    {
        echo $prepend."Hello ".$row['cocname'].$append;
    }

}
?>

Upvotes: 0

Views: 83

Answers (2)

Pradeep Singh
Pradeep Singh

Reputation: 1280

Try this Do write this in signing.php

$query = "SELECT email, password FROM users WHERE email ='$user' and 
password='$pass'" ;  
$result = $connect->query($query);

if ($result->num_rows == 0) {
   header('Location: signin.php?failed=1');
}
if ($result->num_rows > 0) {
    //Email and password matched
    $_SESSION['logged_in'] = true;
    $_SESSION['User'] = $result->fetch_assoc();
}

And in main.php check if user is logged in or not as

if($_SESSION['logged_in']){
   echo "Successfully logged in";
}else{
   echo "Login first to view this page";
}

Don't forget to start session on the top of page. And use $_SESSION["User"] to show user info on main page.

Update : You should submit your form on the "signin.php" itself. If the user is successfully logged in, save a session like $_SESSION["is_login"] = true; and another session containing user info and redirect to the main.php. In the main.php check if "is_login" session is set and is true. If true user is logged in and show there user info from info session. If "is_login" session is not set redirect the user to "signin.php" to signin again.

Update 2

The only solution i found for you is replace

$query = "SELECT cocname FROM users WHERE email=".$_SESSION['email'];

with

$email = $_SESSION['email'];
$query = "SELECT cocname FROM users WHERE email='$email'";

Update 3
Replace
$query = "SELECT cocname FROM users WHERE email=".$_SESSION['email'];
With
$query = "SELECT cocname FROM users WHERE email='{$_SESSION['email']}'";

Upvotes: 0

Akshay
Akshay

Reputation: 2229

Well you need to use sessions here. For all the pages where you need login to work. Add this statement.

session_start();

Now, add session_start(); as the first line of your login page. Update your login page as below.

if ($result->num_rows > 0) 
{
//user logged in
$_SESSION['logged'] =1;
$_SESSION['email']=$user;
//now since this would only return 1 result, you don't need to use a white loop.
 $user_details = $result->fetch_assoc();
}

Read more :- http://www.w3schools.com/php/php_sessions.asp

Upvotes: 1

Related Questions