duanchu0031 2014-06-11 11:42 采纳率: 0%
浏览 33

显示登录的特定用户的删除按钮

I am trying that the user who is logged in can delete his own posts so only he should see the delete button on his posts. I was thinking by my own that I had to bind the user who is logged in to the posts/img ID and then he should see the button. I'm struggling with this a bit so every useful comment is appreciated!

PHP

<?php
$result = $mysqli->query("SELECT users.user_id, users.username,
                                picas.img_id, picas.user_id, picas.name, picas.description, picas.created_at
                          FROM users
                          JOIN picas ON users.user_id = picas.user_id
                          ORDER BY picas.created_at DESC");


while($pica = $result->fetch_assoc()) {

$ses_user = $_SESSION['username'];

echo '<div class="image_post">';

if(isset($ses_user) == $pica['user_id'] && $pica['img_id']) {
  echo '<form action="logic/delete_post.php?id='.$pica['img_id'].'" method="POST">
          <input type="hidden" name="id" value="?id='.$pica['img_id'].'" />
          <input type="submit" name="deleteSubmit" value="Delete" class="delete_post" />
        </form>';
}


    echo '<div class="user_avatar"><img src="avatars/'.$pica['username'].'.jpeg" /></div>
                <div class="user_name">'.$pica['username'].'</div> <br><br><br><br>
                <div class="timeago">'.$diff.'</div>
                <div class="image_description">'.$pica['description'].'</div>
                <img src="'.$pica['name'].'" />

          <div class="clear"></div>
        </div>';

}

?>
  • 写回答

1条回答 默认 最新

  • dongyiyu882684 2014-06-11 11:50
    关注

    Your problem is that you are checking isset($ses_user) == $pica['user_id'] being isset() a function that returns a bool.

    What you want to check is if the current user is the owner.

    The correct condition should be:

    ...
    
    if(isset($ses_user) && ($ses_user == $pica['username']) && $pica['img_id']) {
    echo '<form action="logic/delete_post.php?id='.$pica['img_id'].'" method="POST">
          <input type="hidden" name="id" value="?id='.$pica['img_id'].'" />
          <input type="submit" name="deleteSubmit" value="Delete" class="delete_post" />
        </form>';
    }
    ...
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效