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();
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大