duanchi6377 2014-07-29 19:49
浏览 55
已采纳

无法关注其他用户

So I have a following system that allows you to follow users. So here's the code of follow.php

<?php
error_reporting(0);

session_start();

include_once 'db.php';

$username =  htmlspecialchars($_GET['fuser'], ENT_QUOTES, 'UTF-8');
$follower = $_SESSION['user'];
$type = 'Following';


if($username == $follower){
    header('Location: index.php');
}


if($_SESSION['loggedIn'] == true && $follower != $username){
    $result = $con->prepare("SELECT * FROM followers WHERE follow_to = :post_id");
    $result->bindParam(':post_id', $username);
    $result->execute();
    $reprint = $result->rowCount();
}

if($result->rowCount() < 1){
    //Notifcation handler 
    $notf = $con->prepare("INSERT INTO notifications (from_user, to_user, type) VALUES (:cuser, :tuser, :type)");
    $notf->bindValue(':cuser', $_SESSION['user'], PDO::PARAM_STR);
    $notf->bindValue(':tuser', $username, PDO::PARAM_STR);
    $notf->bindValue(':type', $type, PDO::PARAM_STR);
    $notf->execute();

    //Insert into followers
    $stmt = $con->prepare("INSERT INTO followers (follow_from, follow_to) VALUES (:ff, :ft)");
    $stmt->bindValue(':ff', $follower, PDO::PARAM_STR);
    $stmt->bindValue(':ft', $username, PDO::PARAM_STR);
    $stmt->execute();

}
//Display follower
$stmt1 = $con->prepare("SELECT COUNT(*) AS count FROM followers WHERE follow_to = :username");
$stmt1->bindValue(':username', $username, PDO::PARAM_STR);
$stmt1->execute();
$likes = $stmt1->fetchAll(PDO::FETCH_ASSOC);

$qry = $con->prepare("SELECT follow_from, follow_to FROM followers");
$qry->execute();
$followers = $qry->fetchAll(PDO::FETCH_ASSOC);
header('Location: ' . $_SERVER['HTTP_REFERER']);
exit();

?>

And what happens is when I try following a user all that happens is I get redirect to $_SERVER['HTTP_REFERER']. This makes me think that the last if statement isn't being processed. So here's the table layout

+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| follow_from | varchar(255)     | YES  |     | NULL    |                |
| follow_to   | varchar(255)     | YES  |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+

I think that this might be an issue with my query. Any ideas?

  • 写回答

1条回答 默认 最新

  • dougang7521 2014-07-29 20:24
    关注

    I suggest making the following changes. In your original post, you are looking in the follower tables for ANY record where the follow_to is the person that you wish to follow. Meaning that if someone else is following this user, based on your logic the new following action will not occur.

    I modified your initial sql to check for a record where the logged in user is already matched up as following the requested user.

    I'm adding a $reprint variable initialized to 0. If no user is logged in, nothing will happen. If the user is logged in, but no record exists in the followers table, it inserts the new entries into both followers and notification.

    Otherwise, it doesn't insert and does the remaining parts of your script.

    <?php
    error_reporting(0);
    
    session_start();
    
    include_once 'db.php';
    
    $username =  htmlspecialchars($_GET['fuser'], ENT_QUOTES, 'UTF-8');
    $follower = $_SESSION['user'];
    $type = 'Following';
    
    
    if($username == $follower){
        header('Location: index.php');
    }
    
    $reprint = 0;
    
    if($_SESSION['loggedIn'] == true && $follower != $username){
        $result = $con->prepare("SELECT * FROM followers WHERE follow_to = :post_id and follow_from = :from");
        $result->bindParam(':post_id', $username);
        $result->bindParam(':from', $follower);
        $result->execute();
        $reprint = $result->rowCount();
    }
    
    if($reprint < 1){
        //Notifcation handler 
        $notf = $con->prepare("INSERT INTO notifications (from_user, to_user, type) VALUES (:cuser, :tuser, :type)");
        $notf->bindValue(':cuser', $_SESSION['user'], PDO::PARAM_STR);
        $notf->bindValue(':tuser', $username, PDO::PARAM_STR);
        $notf->bindValue(':type', $type, PDO::PARAM_STR);
        $notf->execute();
    
        //Insert into followers
        $stmt = $con->prepare("INSERT INTO followers (follow_from, follow_to) VALUES (:ff, :ft)");
        $stmt->bindValue(':ff', $follower, PDO::PARAM_STR);
        $stmt->bindValue(':ft', $username, PDO::PARAM_STR);
        $stmt->execute();
    
    }
    //Display follower
    $stmt1 = $con->prepare("SELECT COUNT(*) AS count FROM followers WHERE follow_to = :username");
    $stmt1->bindValue(':username', $username, PDO::PARAM_STR);
    $stmt1->execute();
    $likes = $stmt1->fetchAll(PDO::FETCH_ASSOC);
    
    $qry = $con->prepare("SELECT follow_from, follow_to FROM followers");
    $qry->execute();
    $followers = $qry->fetchAll(PDO::FETCH_ASSOC);
    header('Location: ' . $_SERVER['HTTP_REFERER']);
    exit();
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度