[英]Cant make user following script work accordingly
I feel like I'm missing out on something so little.我觉得我错过了这么少的东西。 I can't seem to logically understand how to display whether they are followed or not at the moment.我似乎无法从逻辑上理解目前如何显示他们是否被关注。 I am able to insert and delete the rows whenever they are in the table, but displaying whether the currently logged in user is following another user already is the problem.只要行在表中,我就可以插入和删除行,但是显示当前登录的用户是否正在关注另一个用户已经是问题所在。 I have a table user_follow:我有一个表 user_follow:
`id` int(11) NOT NULL AUTO_INCREMENT,
`theuserid` int(11) NOT NULL,
`follower` int(11) NOT NULL,
`action` varchar(50) NOT NULL,
profile.php配置文件.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
}
Where theuserid is the user currently logged in and has a session variable $_SESSION["userid"]
and $userthatisfollowed = $row["users_id"];
其中 theuserid 是当前登录的用户,并且有一个会话变量$_SESSION["userid"]
和$userthatisfollowed = $row["users_id"];
is the user that is going to be followed on his profile page.是将在其个人资料页面上关注的用户。
followuser.inc.php 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";
}
I've moved the opening form to outside of the loop and tidied it up a bit for readability, perghaps this is enough to either fix it or understand what's not working.我已将打开的表格移到循环之外并对其进行了一些整理以提高可读性,也许这足以修复它或了解什么不起作用。
<?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 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.