TechnoDiver Posted September 3, 2021 Share Posted September 3, 2021 Hi Freaks, I've been trying to put together subscribe functionality on a project. Been at it for hours and can't make any progress. The categories that a user is subbed to are stored in comma separated string in the database and pulled out into $_SESSION['user_subs'] I have these 2 methods here to start -> <?php private function userSubscribed($cat_id) { $category = $this->getCategoryByID($cat_id); if(in_array($category, $_SESSION['user_subs'])) { echo "user subscribed"; return true; } else { echo "user NOT subscribed"; return false; } } public function displaySubscribedButton($cat_id) { $subscribed = $this->userSubscribed($cat_id); if($subscribed) { $action_btn = "<td><a href='category_posts.php?cat_id=$cat_id' name='sub_btn' class='btn btn-secondary'>Unsubscribe</a>"; } else { $action_btn = "<td><a href='category_posts.php?cat_id=$cat_id' name='sub_btn' class='btn btn-danger text-color- primary'>Subscribe</a>"; } return $action_btn; } They've both been well tested and I know they're working correctly. Then I have this one (which is the problem child) -> <?php public function updateSubscriptionCategory($cat_id, $username) { $category = $this->getCategoryByID($cat_id); $subscribed = $this->userSubscribed($cat_id); print_r($_SESSION['user_subs']); if($subscribed){ unset($_SESSION['user_subs'][$category]); $subs = $_SESSION['user_subs']; $subscribed = false; } else { $new_sub = ",".$category; $subs = array_push($_SESSION['user_subs'], $new_sub); $subscribed = true; } $stmt = $this->conn->prepare("UPDATE ".User::$table." SET subs=? WHERE username=?"); if($stmt) { $stmt->execute([$subs, $username]); } else { echo "you fucked up"; } } I call it from the category file, here -> <?php require_once("assets/initializations.php"); $post_obj = new Post($conn, $username); $cat_obj = new Category($conn, $username); $cat_id = $_GET['cat_id']; if(isset($_POST['sub_btn'])) { $cat_obj->updateSubscriptionCategory($cat_id, $username); // header("Location: category_posts.php?cat_id=$cat_id&cat_title=$cat_title"); } but the page only reloads no changes are made not on the page nor in the db nor do I get any feedback, not from the 'print_r' or the else at the bottom. I feel like it's not even running this method. Was hoping that someone sees something that I've been missing. 8 hours in and I'm still not able to get it to work. TIA for all input Quote Link to comment https://forums.phpfreaks.com/topic/313654-subscribing-a-user-to-a-category/ Share on other sites More sharing options...
ginerjm Posted September 3, 2021 Share Posted September 3, 2021 Proper database design would never use a comma-separated list of items in a field. It would be done as a group of records in a different table that holds the user's id number to connect them all to that subscriber. 1 Quote Link to comment https://forums.phpfreaks.com/topic/313654-subscribing-a-user-to-a-category/#findComment-1589622 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.