my $user['id'] & $username is not working??... sometime working but sometime not

0 votes
asked Jun 10, 2016 in PHP Frameworks by isha_ijp (120 points)

My code : here on this line <?php if($user['id'] == $uid ){ ?> its not working but on this line

<td><a href="update.php?id=<?php echo $user['id']; ?> -- id is working 

                <tr>
                    <td>Website</td>
                    <td><?php echo $user['website']; ?></td>
                <tr>
                <?php if($user['id'] == $uid ){ ?>
                <tr>
                    <td>Update Profile</td>
                    <td><a href="update.php?id=<?php echo $user['id']; ?>">Edit Profile</a></td>
                </tr>               
               <?php } } ?>
            </table>
<div class="back">
    <a href="index.php"><img src="img/back.png" alt="back" /></a>
</div>
            </div>

this is my function.php

<?php
require "config.php";

class LoginRegistration{

    function __construct(){
        $database = new DatabaseConnection();
    }

    public function registerUser($username,$password,$name,$email,$website){
        global $pdo;

        $query = $pdo->prepare('SELECT id FROM users WHERE username = ? AND email = ?');
        $query->execute(array($username, $email));
        $num = $query->rowCount();

        if($num == 0){
            $query = $pdo->prepare("INSERT INTO users (username,password,name,email,website) VALUES (?, ?, ?, ?, ?)");
            $query->execute(array($username, $password, $name, $email, $website));
            return true;
        } else {
            print "<span style='color:#e53d7'>;Error....username/email already used</span>";
        }
        }
    public function loginUser($email, $password){
        global $pdo;
        $query = $pdo->prepare("SELSECT id, username FROM users WHERE email =? AND password =?");
        $query->execute(array($email, $password));
        $userdata = $query->fetch();

        $num = $query->rowCount();
        if($num == 0){
            session_start();
            $_SESSION['login'] = true;
            $_SESSION['uid'] = $userdata['id'];
            $_SESSION['uname'] = $userdata['username'];
            $_SESSION['login_msg'] = 'Login successfully...';

                return true;
        } else {
                return false;
        }
    }   

    public function getAllusers(){
        global $pdo;
        $query = $pdo->prepare("SELECT * FROM users ORDER BY id DESC");
        $query->execute();
        return $query->fetchALL(PDO::FETCH_ASSOC);
    }

    public function getSession(){
        return @$_SESSION['login'];
    }

    public function getUsername($uid){
        global $pdo;

        $query = $pdo->prepare("SELECT name FROM users where id = ? ");
        $query->execute(array($uid));
        $result = $query->fetch();
        echo $result['name'];
    }

    public function getUserById($id){
        global $pdo;

        $query = $pdo->prepare("SELECT * FROM users where id = ? ");
        $query->execute(array($id));
         return $query->fetchALL(PDO::FETCH_ASSOC);     
    }


}
?>
}

on index page m having same problem $username is not working.... others working properly

<h2>Welcome <?php echo $username; ?></h2> 

1 Answer

0 votes
answered Sep 10, 2016 by shimul (680 points)
edited Sep 10, 2016 by shimul

you can try this way

<h2>Welcome <?php echo $username['name']?></h2>

Note: $username = your object variable

if you want login username

<h2>Welcome<?php echo $_SESSION['username']?><h2>

Related questions

0 votes
2 answers
0 votes
1 answer
+2 votes
2 answers
0 votes
1 answer
asked May 14, 2016 in PHP by alamindit (580 points)

722 questions

657 answers

179 comments

4,937 users

Welcome to IDB Scholars Forum, where you can ask questions and receive answers from other members of the community. It's 100% free.
আইডিবি স্কলারস ফোরামে আপনাকে স্বাগত - প্রোগ্রামিং, ডিজাইন, ডেভেলপমেন্ট, ব্লগিং, ইন্টারনেটসহ তথ্য প্রযুক্তির যাবতীয় বিষয় নিয়ে আলোচনার একটি মুক্ত মঞ্চ হলো এই ফোরাম । এখানে আইসিটি বিষয়ক আপনার প্রশ্ন, উত্তর এবং অভিজ্ঞতা শেয়ার করুন, আলোচনা করুন এবং সমাধান নিন। দেশের তরুণ প্রজন্মকে আইসিটিতে আগ্রহী করাই আমাদের মূল লক্ষ্য।
** ইংলিশ বা বাংলা যে কোনো ভাষায় আপনি এই ফোরামে আলোচনা করতে পারবেন। তবে বাংলাকে ইংরেজি অক্ষরে লিখবেন না।
  1. month7jeff

    100 points

    0 answers

  2. PrincessVill

    100 points

    0 answers

  3. recess8hip

    100 points

    0 answers

  4. editorsong1

    100 points

    0 answers

  5. KaiSilva2538

    100 points

    0 answers

...