0

In this script im trying to check if a user exist. if it does, display if it doesnt display error message. The problem is I only get the error message so by Guessing the script is not returning anything correctly. Please kindly take a look. Thanks.

public function check_user($uid){
        $sql        =   "SELECT * from users WHERE uid= :uid ";
        $q = $this->db->prepare($sql);
        $q->execute(array(':uid'=>$uid));
        $result = $q->fetch(PDO::FETCH_ASSOC);
        return $result;
}

profile.php

    <?php
    ini_set('display_errors', 1); 
    error_reporting(E_ALL);
    session_start();

    include_once('php/classes/db_config.php');
    include_once('php/classes/class.user.php');

    $user1 = new User($con);
    $is_loggedin = (isset($_SESSION['uid']));
    $is_uid = (!empty($_GET['uid']) && is_numeric($_GET['uid']));
    $def_uid = ($is_uid) ? $_GET['uid'] : $_SESSION['uid'];
    $user_valid = ($is_uid == true) ? $user1->check_user($def_uid) : 1;

    @$name_id = $_SESSION['user']['uid'];
    $name = $_SESSION['user']['uname'];
    $fullname = $_SESSION['user']['fullname'];
    $bio = $_SESSION['user']['bio'];
    $time = date("Y-m-d H:i:s");

    if (isset($_POST['logout'])) {
        session_destroy();
        header('Location: index.php');
        exit;
    }

    if (isset($_POST['area_sub'])) {
        if (empty($_POST['area'])) {
            echo "<script>alert('Empty area field.')</script>";
        }else{
            $uid = $_GET['uid'];
            if ($uid == '' || $uid == 0) {
                $uid = $name_id;
            }
            $user1->post($name_id, $uid, $name, $_POST['area'], $time);
        }
    }
    if($is_loggedin){
        $sql = "SELECT * FROM follow_req WHERE user_two_req= :user_two_req";
        $query = $con->prepare($sql);
        $query->execute(array( ':user_two_req' => $name_id));
        $result = $query->fetchALL(PDO::FETCH_ASSOC);
    }


?>

            // If user is valid
            if($user_valid == 1) {
                // User is logged in user
                if($def_uid == $name_id) {
                    include_once 'php/classes/profile_func.php';
                } 
                include_once 'php/classes/user_info.php';

            }else{?>
                <h2>No Such User Exists</h2>
                <h3>Please select a different user or <a href='index.php'>Login</a></h3>
                <?php if($is_loggedin == true){ ?>
                        <h3>Go Back to <a href="profile.php?uid=<?php echo $name_id;?>">My Profile</a></h3>
                    <?php
                }
            } ?>
7
  • Does it run when not set in a function? If it does, then it's a scope issue. Commented Nov 20, 2014 at 15:52
  • Yes it does run if i do the following :: public function check_user($uid){ $sql = "SELECT * from users WHERE uid= :uid "; $q = $this->db->prepare($sql); $q->execute(array(':uid'=>$uid)); return $q; } Commented Nov 20, 2014 at 15:54
  • What do you mean by if you "dot the following"? Commented Nov 20, 2014 at 15:55
  • If i return the return $q it works but if the user is invalid it doesnt give me invalid user. Edited my answer before Commented Nov 20, 2014 at 15:58
  • Then wrap that code in your function with an if/else Commented Nov 20, 2014 at 15:59

1 Answer 1

1

Fixed it.

if($user_valid == 1) {
            // User is logged in user
            if($def_uid == $name_id) {
                include_once 'php/classes/profile_func.php';
            } 
            include_once 'php/classes/user_info.php';

        }else{?>
            <h2>No Such User Exists</h2>
            <h3>Please select a different user or <a href='index.php'>Login</a></h3>
            <?php if($is_loggedin == true){ ?>
                    <h3>Go Back to <a href="profile.php?uid=<?php echo $name_id;?>">My Profile</a></h3>
                <?php
            }
        }

changed it to:

--> if($user_valid == true) {
            // User is logged in user
            if($def_uid == $name_id) {
                include_once 'php/classes/profile_func.php';
            } 
            include_once 'php/classes/user_info.php';

        }else{?>
            <h2>No Such User Exists</h2>
            <h3>Please select a different user or <a href='index.php'>Login</a></h3>
            <?php if($is_loggedin == true){ ?>
                    <h3>Go Back to <a href="profile.php?uid=<?php echo $name_id;?>">My Profile</a></h3>
                <?php
            }
        }
Sign up to request clarification or add additional context in comments.

2 Comments

In PHP true == 1 So I doubt that was the fix
Actually it was @DarkBee. I mean it's working as of now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.