简体   繁体   English

仅显示特定用户帖子的超链接

[英]show hyperlink only for a certain user's posts

I have a posts table like this: 我有一个这样的帖子表:

post_id            user_id       title            date
   12                  1           abc          7/20/2014
   13                  1           cde          7/21/2014
   14                  2           fgh          7/22/2014

And a users table like this: 和一个用户表是这样的:

user_id          username         email                    password
   1               name1          email1@domain.com         ******
   2               name2          email2@domain.com         ******

The user_id in the posts table is the foreign key of the users table. posts表中的user_idusers表的外键。

Note: 注意:

Assume that I store the user's session successfully by using $_SESSION['user_id'] . 假设我使用$_SESSION['user_id']成功存储了用户会话。

Assume that I can echo all 3 post titles along with their usernames successfully too. 假设我也可以成功回应所有3个帖子标题及其用户名。

Now I would like to echo the edit hyperlink ( <a href="edit.com">Edit</a> ) for the post titles of a certain user, for example, user_id 1 or 2 after he's logged in. It means that if the user named name1 signs into his account and browses the post whose id is 14, he cannot see the edit hyperlink because it belongs to the another user named name2 , whose session id, if any, should be unableable now, and vice versa. 现在,我想显某个用户的帖子标题的编辑超链接<a href="edit.com">Edit</a> ,例如,用户登录后的user_id 1 or 2这意味着如果名为name1的用户name1其帐户并浏览ID为14的帖子,则他将看不到编辑超链接,因为它属于另一个名为name2用户,该用户的会话ID(如果有的话)现在将无法使用,反之亦然。

For me, this is my code: 对我来说,这是我的代码:

$q = "SELECT title, post_id, p.user_id AS currentuser
      FROM posts AS p
      INNER JOIN users AS u USING (user_id)
      ORDER BY date ASC
      ";
$r = mysqli_query ($dbc, $q);

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

$uid = isset($_SESSION['user_id']);// variable for the seesion

$current_user = $row['currentuser'];// variable for the certain user ID

          if($uid && $uid == $current_user){      
              echo "<a href=\"http://edit.com\"> Edit </a></div>";
            }

Then I test it, you know, it shows the edit hyperlink in every post title, but not what i expected. 然后,我对其进行测试,您知道,它在每个帖子标题中都显示了编辑超链接,但没有我所期望的。

Can you help? 你能帮我吗? Thanks 谢谢

$uid = isset($_SESSION['user_id']);

This results to either true or false. 结果为真或假。 try 尝试

$uid = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : -1;

What I did was use a ternary operator so if the session variable is set, you set it to it otherwise you set it to -1, which most probably does not exsist in a table id column 我所做的是使用三元运算符,因此如果设置了会话变量,则将其设置为它,否则将其设置为-1,这很可能不存在于表id列中

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

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