[英]Cant make user following script work accordingly
我覺得我錯過了這么少的東西。 我似乎無法從邏輯上理解目前如何顯示他們是否被關注。 只要行在表中,我就可以插入和刪除行,但是顯示當前登錄的用戶是否正在關注另一個用戶已經是問題所在。 我有一個表 user_follow:
`id` int(11) NOT NULL AUTO_INCREMENT,
`theuserid` int(11) NOT NULL,
`follower` int(11) NOT NULL,
`action` varchar(50) NOT NULL,
配置文件.php
<div class="userprofilecontainer">
<?php
$userurl = $_GET['user'];
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$stmt = $conn->prepare('SELECT * FROM users LEFT JOIN user_follow on users.users_id = user_follow.theuserid WHERE users_username = ?;');
$stmt->bind_param('s', $userurl);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_array()){
$actionF = $row['action'];
$userthatisfollowed = $row["users_id"];
if(isset($_SESSION["userid"]) && $_SESSION["userid"] != $userthatisfollowed) {
?>
<form action="<?php echo htmlspecialchars("includes/followuser.inc.php");?>" id="followform" method="post">
<?php
$sub = false; //Boolean var which states if subscribed or not
if (isset($row['action'])){ //If action is Followed??
$sub = true; //If row is found, they are subscribed, so set $sub to true
}
if($sub){
$subscribe_status = "Follow";
echo "<button name='submitFollow' id ='followbutton' type='submit'><p>Follow</p></button>";
}
else{
$subscribe_status = "Unfollow";
echo "<button name='submitUnfollow' id ='followbutton' type='submit'><p>Unfollow</p></button>";
}
?>
<input name="action" type="hidden" value="<?php echo $subscribe_status; ?>"/>
<input type="hidden" name="theuserid" value="<?php echo $_SESSION["userid"] ?>">
<input type="hidden" name="follower" value="<?php echo $userthatisfollowed; ?>">
</form>
<?php
}
其中 theuserid 是當前登錄的用戶,並且有一個會話變量$_SESSION["userid"]
和$userthatisfollowed = $row["users_id"];
是將在其個人資料頁面上關注的用戶。
followuser.inc.php
<?php
require_once 'dbh.inc.php';
require_once 'functions.inc.php';
if (isset($_POST["submitFollow"])) {
$action = $_POST['action'];
$userthatisfollowed = $_POST["follower"];
$theuserid = $_POST["theuserid"];
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$stmt = $conn->prepare('INSERT INTO user_follow (follower, theuserid, action) VALUES (?,?,?)');
$stmt->bind_param('sss', $userthatisfollowed, $theuserid, $action);
$stmt->execute();
$followed = $_SESSION["followed"];
$stmt->close();
header("location: ../home.php");
exit();
} else if (isset($_POST["submitUnfollow"])) {
$userthatisfollowed = $_POST["follower"];
$theuserid = $_POST["theuserid"];
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$stmt = $conn->prepare('DELETE user_follow FROM user_follow WHERE follower = ? AND theuserid = ?');
$stmt->bind_param('ss', $userthatisfollowed, $theuserid);
$stmt->execute();
$stmt->close();
header("location: ../home.php");
exit();
} else {
echo "DID NOT WORK";
}
我已將打開的表格移到循環之外並對其進行了一些整理以提高可讀性,也許這足以修復它或了解什么不起作用。
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$stmt = $conn->prepare(
'SELECT * FROM users
LEFT JOIN
user_follow on users.users_id = user_follow.theuserid
WHERE users_username = ?;'
);
$stmt->bind_param('s', $_GET['user']);
$stmt->execute();
$result = $stmt->get_result();
echo '<form action="' . htmlspecialchars("includes/followuser.inc.php") . '" id="followform" method="post">';
while ($row = $result->fetch_array()) {
// print_r($row);
if (isset($_SESSION["userid"]) && $_SESSION["userid"] != $row["users_id"]) {
if (isset($row['action']) && (bool)$row['action']) {
$subscribe_status = "Follow";
} else {
$subscribe_status = "Unfollow";
}
echo "<button name='submit" . $subscribe_status . "' id ='followbutton' type='submit'><p>" . $subscribe_status . "</p></button>";
}
}
echo '</form>';
followuser.inc.php
<?php
require_once 'dbh.inc.php';
require_once 'functions.inc.php';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
if (isset($_POST["submitFollow"])) { // either is or is not set
$stmt = $conn->prepare('INSERT INTO user_follow (follower, theuserid, action) VALUES (?,?,?)');
$stmt->bind_param('sss', $_POST["follower"], $_POST["theuserid"], $_POST['action']);
$stmt->execute();
$followed = $_SESSION["followed"];
$stmt->close();
} else if (isset($_POST["submitUnfollow"])) {
$stmt = $conn->prepare('DELETE user_follow FROM user_follow WHERE follower = ? AND theuserid = ?');
$stmt->bind_param('ss', $_POST["follower"], $_POST["theuserid"]);
$stmt->execute();
$stmt->close();
} else {
// neither $_POST["submitFollow"] nor $_POST["submitUnfollow"] is set
}
header("location: ../home.php");
exit();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.