简体   繁体   English

关注/取消关注工具PHP / mySQL

[英]Follow/unfollow tool PHP/mySQL

I am sure I am missing something obvious or the mySQL code I have written for this tool is incorrect. 我确定我缺少明显的东西,或者我为此工具编写的mySQL代码不正确。

What I have got is a site that allow people to follow each other. 我得到的是一个允许人们互相关注的网站。 If they are following a user they should see updates from them in their user dashboard. 如果他们关注用户,则应该在用户仪表板中看到他们的更新。

I have had to tie several tables together in mySQL query to get the relevant information. 我不得不在mySQL查询中将几个表捆绑在一起以获取相关信息。

Here are the tables 这是桌子

users 使用者

ID                Username           Password
-------------|---------------|------------------
    1              User1         UserPass
    2              User2         UserPass
    3              User3         UserPass

user_details user_details

ID                UserID           UserPhoto
-------------|---------------|------------------
    1              1            User1photo.jpg
    2              2            User2photo.jpg
    3              3            User3photo.jpg

userstatusposts 用户状态帖子

UserStatusID       UserID           status
-------------|---------------|------------------
    1              1            Hey My first post
    2              2            Woah this is cool
    3              3            It doesnt work

followers 追随者

followid       followerUserID   beingFollowedUserID
-------------|---------------|------------------
    1              3            1

There are more cols and rows in these tables but this is a basic form for the question. 这些表中有更多列和行,但这是该问题的基本形式。

As you can see from the followers table User3 is following User1 and should therefore be able to see the posts they have made in userstatusposts, the reason user details and users also need tying in is so I can display the users photo and the users username 从关注者表中可以看到,User3在关注User1,因此应该能够看到他们在userstatusposts中发布的帖子,原因是用户详细信息和用户也需要绑定,因此我可以显示用户照片和用户名

The SQL I have at the moment that isn't working is: 我目前无法使用的SQL是:

SELECT * FROM userstatusposts 
                JOIN followers ON userstatusposts.userid = followers.followeruserid 
                JOIN users ON userstatusposts.userid = users.id 
                JOIN user_details ON userstatusposts.userid = user_details.userid 
                WHERE followers.beingFollowedUserID='$userid' 
                ORDER BY userstatusposts.userstatusid DESC
                LIMIT 10

However this is all tied together wrong as I see the posts of the wrong users when it is implemented in to my PHP code ($userid is the logged in user). 但是,这一切都是错的,因为在将其实现到我的PHP代码中时,我看到了错误的用户的帖子($ userid是登录的用户)。

PHP Page: PHP页面:

 <?php
                $sql = "SELECT * FROM userstatusposts 
                JOIN followers ON userstatusposts.userid = followers.followeruserid 
                JOIN users ON userstatusposts.userid = users.id 
                JOIN user_details ON userstatusposts.userid = user_details.userid 
                WHERE followers.beingFollowedUserID='$userid' 
                ORDER BY userstatusposts.userstatusid DESC
                LIMIT 10
                ";

                $result = $conn->query($sql);
                $rowcount=mysqli_num_rows($result);
                if ($rowcount === 0) {
                  echo '<li style="list-style-type: none;"><p>Your not currently folllowing anyone.</p></li>';
                } else {
                  while($row = $result->fetch_assoc()) {
                    if ($row['posttype'] == 'message') {
                      echo '<li style="list-style-type: none;"><a href="#"><img src="userimg/'.$row['userphoto'].'" height="90px" width="90px"></a><h3><a href="#">'.$row['username'].'</a></h3><small>17/06/2014</small><p>'.$row['status'].'</p></li>';
                    }
                    else if ($row['posttype'] == 'map') {
                      echo '<li style="list-style-type: none;"><a href="#"><img src="userimg/'.$row['userphoto'].'" height="90px" width="90px"></a><h3><a href="#">'.$row['username'].'</a></h3><p>has recently added <b>'.$row['status'].'</b> to there travel map</p></li>';
                    }
                    else if ($row['posttype'] == 'like') {
                      echo '<li style="list-style-type: none;"><a href="#"><img src="userimg/'.$row['userphoto'].'" height="90px" width="90px"></a><h3><a href="#">'.$row['username'].'</a></h3><p>has recently liked a trip report called <b>'.$row['status'].'</b></p></li>';
                    }
                    else if ($row['posttype'] == 'report') {
                      echo '<li style="list-style-type: none;"><a href="#"><img src="userimg/'.$row['userphoto'].'" height="90px" width="90px"></a><h3><a href="#">'.$row['username'].'</a></h3><p>has recently shared a trip report called <b>'.$row['status'].'</b></p></li>';
                    }
                    else {
                      echo 'We are currently expirencing a few diffculties';
                    }
                  }

                }
              ?>

I am aware there are other cols being used here but they are in the tables listed above I have just left them out for the question. 我知道这里还使用了其他列,但它们在上面列出的表格中,我只是在回答问题。

Any suggestions why my SQL code is bringing back the wrong information, is it something glaringly obvious I have over looked? 有什么建议为什么我的SQL代码会带回错误的信息,这是我已经看过的显而易见的东西吗?

Your query returns all the status of followers, not the status of followed. 您的查询返回所有关注者状态,而不是关注状态。

You should change 你应该改变

WHERE followers.beingFollowedUserID='$userid' 

to

WHERE followers.followerUserID = '$userid' 

In addition, this is unrelated to your question, but your code has a security problem. 此外,这与您的问题无关,但是您的代码存在安全问题。 You should sanitize $userid first before using it in your query. 您应该先清理$ userid,然后才能在查询中使用它。

$userid = (int)$userid

// .. now $userid is safe to use in $sql

The issue was one of the JOINS was looking to the wrong field. 问题是JOINS寻求错误的领域之一。

The SQL should of been: SQL应该是:

JOIN followers ON userstatusposts.userid = followers.beingFollowedUserID 

So the full statement is 所以完整的陈述是

SELECT * FROM userstatusposts 
                JOIN followers ON userstatusposts.userid = followers.beingFollowedUserID 
                JOIN users ON userstatusposts.userid = users.id 
                JOIN user_details ON userstatusposts.userid = user_details.userid 
                WHERE followers.followerUserID = '$userid'  
                ORDER BY userstatusposts.userstatusid DESC
                LIMIT 10

This was solved by a friend of mine not me, but thought I would share the answer for anyone interested. 这是我的一个朋友解决的,而不是我,但我认为我会与有兴趣的人分享答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM