页面继续加载。 PHP

时间:2014-11-20 05:22:53

标签: php oop pdo

我创建了一个脚本,社交网络。我有个问题。如果用户上传了图片我应该显示图片,如果没有,那么它应该显示默认图片。问题是,每次重新加载页面时都会继续加载,用户图片不能正确显示。如果我检查元素,图片网址是正确的,但由于页面继续加载,图片不显示。只有在else语句中我才会有循环。我试过 - 每个循环但同样的问题。任何帮助将不胜感激。

我所指的功能是profile_photo();

profile.php

    <?php
    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']));

    //set uid
    $def_uid = ($is_uid) ? $_GET['uid'] : $_SESSION['uid'];

    // User valid check
    // ### what does 1 mean, perhaps define a constant or use true/false instead
    $user_valid = ($is_uid == true) ? $user1->check_user($def_uid) : 1;

    // ## if $_SESSION['user'] doesn't exist these assignments will generate warnings
    $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 = $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);
    }


?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Only Simple Albanian Social Network, with no ads</title>
    <link rel="stylesheet" href="css/profile.css"/>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    </head>

    <body>
    <?php if($is_loggedin !== true) { 
            include_once 'php/common/head.php'; ?>
    <?php }?>

    <?php
    // If there is a propper UID available
    if(($is_loggedin == true) || ($is_uid == true)) { ?>

    <?php if ($is_loggedin == true){ ?>

    <div id="menu">
        <div id="menu_wrapper">
            <div id="left_menu">

            </div>
            <div id="right_menu">
                <div id='drop-zone'><?php
                    if($result == false){
                        echo "<h4 class='request'>0</h4>";
                    }else{ 
                        echo "<a href='#' class='request_more'>".count($result)."</a>";
                    }   
                ?></div>
                <form action='' method='post'>
                    <input type='hidden' name='logout' value='true' />
                    <input type='submit' name='submit' value='Logout' id='btn'>
                </form>
            </div>
        </div>
    </div>

    <?php } ?>

    <div id="profile_wrapper">

        <div class="wrapper">

            <div id="profile">  

            <?php

            // If user is valid
            if($user_valid == 1) {
                // User is logged in user
                if($def_uid == $_SESSION['uid']) {
                    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
                }
            } ?>

<?php }else{?>
        <h1>Invalid User</h1>
        <h3>You must be <a href="index.php">Logged IN</a> to see your profile OR you must select a profile (uid) to view.</h3>
<?php } ?>
            </div>
        </div>
    </div>
<?php if($is_loggedin != true){ ?>
    <div id="footer">
        <h4>Per momentin vetem njerezit qe do perzgjidhen do kene akses.</h4>
        <a href="#">
            <div id="button">Kerkese Hyerje</div>
        </a>
        </div>
<?php } ?>

<script>
function LookUp(IdElem,URL){
        $("#drop-zone").html('<div class="loader"></div>');
        $.ajax({
                url: URL,
                cache: false,
                success: function(result) {
                        $("#drop-zone").html(result);
                    }
            });
}
$(document).ready(function() {
        $("a.request_more").click(function(){
            LookUp(this,'requests.php');
        });
});
</script>

</body>
</html>

class.user.php

    <?php
class User
    {
        public  $db;
        public  $error;

        public function __construct($con){
                $this->db = $con;
        }

        /*** for login process ***/
        public function check_login($username='', $password=''){
                // Validate that your email is a real one
                if(filter_var($username,FILTER_VALIDATE_EMAIL) !== false) {
                        $password   =   md5($password);
                        $sql        =   "SELECT uid from users WHERE (uemail='$username' or uname='$username') and upass = '$password'";
                        $result     =   $this->db->Fetch($sql);

                            if ($result !== 0) {
                                    // this login var will use for the session thing
                                    $_SESSION['emailusername']  =   $result[0]['uemail'];
                                    $_SESSION['uid']            =   $result[0]['uid'];
                                    $_SESSION['user']           =   $this->get_fullname($result[0]['uid'],0);
                                    $_SESSION['login']          =   true;
                                }
                            else
                                $this->error['Invalid Account'] = '<h3 id="error">Invalid Username or Password</h3>';
                    }
                else
                    $this->error['Email'] = '<h3 id="error">Invalid Email Address</h3>';

                return  (!isset($_SESSION['emailusername']))? false:true;
            }

        /*** for showing the username or fullname ***/
        public function get_fullname($uid, $write = 1){

                // --> You can prepare, bind, and execute your values here replacing what you have now....<--

                $sql                =   "SELECT * FROM users WHERE uid = '$uid'";
                $user_data          =   $this->db->Fetch($sql);

                if($user_data !== 0) {
                        $user['uname']      =   $user_data[0]['uname'];
                        $user['fullname']   =   $user_data[0]['fullname'];
                        $user['uemail']     =   $user_data[0]['uemail'];
                        $user['uid']        =   $user_data[0]['uid'];
                        $user['bio']        =   $user_data[0]['bio'];

                        // This gives the option of returning an array (setting session array) or echoing
                        if($write == 1)
                            echo implode("<br />",$user);
                        else
                            return $user;
                    }
            }

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

        /*** starting the session ***/
        public function get_session()
            {
                return $_SESSION['login'];
            }

        public function user_logout()
            {
                $_SESSION['login'] = FALSE;
                session_destroy();
            }
        public function check_req($is_uid, $def_uid){
            $sql        =   "SELECT * from follow_req WHERE user_one_req = :user_one_req AND user_two_req = :user_two_req";
            $q = $this->db->prepare($sql);
            $q -> execute(array(':user_one_req'=>$is_uid, 'user_two_req'=>$def_uid));
            $result = $q->fetchALL(PDO::FETCH_ASSOC);
            return $result;
        }
        public function insert($is_uid, $def_uid){
            $sql        =   "INSERT INTO follow_req (user_one_req, user_two_req) VALUES (:user_one_req, :user_two_req)";
            $q = $this->db->prepare($sql);
            $q -> execute(array(':user_one_req'=>$is_uid, ':user_two_req'=>$def_uid));
            $result = $q->fetchALL(PDO::FETCH_ASSOC);
            return $result;
        }
        public function delete($is_uid, $def_uid){
            $sql        =   "DELETE FROM follow_req WHERE user_one_req = :user_one_req AND user_two_req = :user_two_req";
            $q = $this->db->prepare($sql);
            $q -> execute(array(':user_one_req'=>$is_uid, ':user_two_req'=>$def_uid));
            $result = $q->fetchALL(PDO::FETCH_ASSOC);
            return $result;
        }
        public function checkimg($img_id){
            $sql        =   "SELECT * from users WHERE img_id = '$img_id'";
            $result     =   $this->db->Fetch($sql);
            $count_row  =   ($result !== 0)? count($result): 0;
        }
        public function img($img_id, $part){
            $sql        =   "UPDATE users SET img_id='$img_id', image='$part' WHERE uid=".$_SESSION['uid']."";
            $q = $this->db->prepare($sql);
            $q -> execute(array(':img_id'=>$img_id, ':image'=>$part));
            $result = $q->fetchALL(PDO::FETCH_ASSOC);
            return $result;
        }
        public function check_friends($is_uid, $def_uid){
            $sql        =   "SELECT * from follow WHERE (user_one = :user_one AND user_two = :user_two) OR (user_one = :user_two AND user_two = :user_one)";
            $q = $this->db->prepare($sql);
            $q -> execute(array(':user_one'=>$is_uid, ':user_two'=>$def_uid));
            $result = $q->fetchALL(PDO::FETCH_ASSOC);
            return $result;
        }
        public function del_friends($is_uid, $def_uid){
            $sql        =   "DELETE FROM follow WHERE (user_one = :user_one AND user_two = :user_two) OR (user_one = :user_two AND user_two = :user_one)";
            $q = $this->db->prepare($sql);
            $q -> execute(array(':user_one'=>$is_uid, ':user_two'=>$def_uid));
            $result = $q->fetchALL(PDO::FETCH_ASSOC);
            return $result;
        }
        public function checking($def_uid, $is_uid){
            $sql        =   "SELECT * from follow_req WHERE user_one_req = :user_one_req AND user_two_req = :user_two_req";
            $q = $this->db->prepare($sql);
            $q -> execute(array(':user_one_req'=>$def_uid, ':user_two_req'=>$is_uid));
            $result = $q->fetchALL(PDO::FETCH_ASSOC);
            return $result;
        }
        public function accept_req($def_uid, $is_uid){
            $sql        =   "INSERT INTO follow ( user_one, user_two) VALUES ( :user_one, :user_two)";
            $q = $this->db->prepare($sql);
            $q -> execute(array(':user_one'=>$def_uid, ':user_two'=>$is_uid));
            $result = $q->fetchALL(PDO::FETCH_ASSOC);
            return $result;
        }
        public function delete_req($def_uid, $is_uid){
            $sql        =   "DELETE FROM follow_req WHERE user_one_req = :user_one_req AND user_two_req = :user_two_req";
            $q = $this->db->prepare($sql);
            $q -> execute(array(':user_one_req'=>$def_uid, ':user_two_req'=>$is_uid));
            $result = $q->fetchALL(PDO::FETCH_ASSOC);
            return $result;
        }
        public function post($a_id='', $r_id='', $name, $area='', $time){
            $sql = $this->db->prepare("INSERT INTO post (user_id, rec_id, user, area, time) VALUES ( ?, ?, ?, ?, ?)");
            $sql -> bindParam(1, $a_id);
            $sql -> bindParam(2, $r_id);
            $sql -> bindParam(3, $name);
            $sql -> bindParam(4, $area);
            $sql -> bindParam(5, $time);
            $sql -> execute();
        }
        public function profile_photo($name_id){
            $sql = "SELECT * FROM users WHERE img_id= :img_id";
            $q = $this->db->prepare($sql);
            $q->execute(array(':img_id'=>$name_id));
            $result = $q->fetchALL(PDO::FETCH_ASSOC);
            if($result[0]['img_id'] == $name_id){
                echo "<img id='profile_img' src='test/upload/".$name_id."_".$result[0]['image']."'/>";
            }else{  
                echo "<img id='profile_img' src='classversiondontdelete/images/default/profile_icon.gif'/>";
            }
            return $result;

        }
        public function getResults(){
            if($_GET['uid'] == ''){
                $result = $this->db->Fetch("SELECT * FROM post WHERE rec_id = ".$_SESSION['uid']." ");
            }else if($_GET['uid'] == 0){
                $result = $this->db->Fetch("SELECT * FROM post WHERE rec_id = ".$_SESSION['uid']." ");
            }else{
                if(is_numeric($_GET['uid']))
                    $result = $this->db->Fetch("SELECT * FROM post WHERE rec_id = '".$_GET['uid']."'");
            }
            if(is_array($result)){

            }
        }
    }
?>  

1 个答案:

答案 0 :(得分:3)

解决。我的错误是:

public function profile_photo($name_id){
        $sql = "SELECT * FROM users WHERE img_id= :img_id";
        $q = $this->db->prepare($sql);
        $q->execute(array(':img_id'=>$name_id));
        $result = $q->fetchALL(PDO::FETCH_ASSOC);
        if($result[0]['img_id'] == $name_id){
            echo "<img id='profile_img' src='test/upload/".$name_id."_".$result[0]['image']."'/>";
        }else{  
            echo "<img id='profile_img' src='classversiondontdelete/images/default/profile_icon.gif'/>";
        }
        return $result;

}

我应该放$_SESSION而不是$name_id

public function profile_photo($name_id){
        $sql = "SELECT * FROM users WHERE img_id= :img_id";
        $q = $this->db->prepare($sql);
        $q->execute(array(':img_id'=>$name_id));
        $result = $q->fetchALL(PDO::FETCH_ASSOC);
        if($result[0]['img_id'] == $_SESSION['uid']){
            echo "<img id='profile_img' src='test/upload/".$name_id."_".$result[0]['image']."'/>";
        }else{  
            echo "<img id='profile_img' src='classversiondontdelete/images/default/profile_icon.gif'/>";
        }
        return $result;

    }