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:
`id` int(11) NOT NULL AUTO_INCREMENT,
`theuserid` int(11) NOT NULL,
`follower` int(11) NOT NULL,
`action` varchar(50) NOT NULL,
profile.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"];
is the user that is going to be followed on his profile page.
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
<?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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.