所以我有一个允许你关注用户的系统。所以这是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();
?>
当我尝试跟踪用户所发生的一切时,我会重定向到$_SERVER['HTTP_REFERER']
。这让我觉得最后的if
语句没有被处理。所以这是表格布局
+-------------+------------------+------+-----+---------+----------------+
| 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 | |
+-------------+------------------+------+-----+---------+----------------+
我认为这可能是我查询的问题。有任何想法吗?
答案 0 :(得分:1)
我建议进行以下更改。在您的原始帖子中,您正在查看关注者表中的任何记录,其中follow_to是您希望关注的人。这意味着如果其他人关注此用户,则根据您的逻辑,将不会发生新的以下操作。
我修改了您的初始sql,以检查已登录用户已按照请求的用户进行匹配的记录。
我添加了一个初始化为0的$ reprint变量。如果没有用户登录,则不会发生任何事情。如果用户已登录,但关注者表中不存在任何记录,则会将新条目插入关注者和通知中。
否则,它不会插入并执行脚本的其余部分。
<?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();
?>