在php foreach循环中根据用户SESSION隐藏和显示按钮

我试图不对未将图像上传到网站的用户显示删除按钮,并且我希望仅对上传特定图像的用户显示删除按钮。问题是,它在 foreach 循环中,我尝试过

if($user_id == $_GET['id']

但它显示了每个按钮,但是当我把

if($user_id != $_GET['id'])

所有按钮消失。


梦里花落0921
浏览 68回答 1
1回答

不负相思意

如果将这 2 个查询更改为一个查询,您将得到一个包含 img 和用户 id 的结果集,然后您可以使用它来与登录的用户进行比较//$subjects = $db_conn->prepare("SELECT img FROM images");//$subjects->setFetchMode(PDO::FETCH_ASSOC);//$subjects->execute();//$stmt = $db_conn->prepare("SELECT user_id FROM images");//$stmt->execute();//$nesto=$stmt->fetchAll(PDO::FETCH_ASSOC);替换为$result = $db_conn->query("SELECT img, user_id FROM images");$subjects = $result->fetchAll(PDO::FETCH_ASSOC);然后在你的按钮周围你可以做<?php&nbsp; &nbsp; // If this user uploaded this image they are allowed to remove it&nbsp; &nbsp; if ($subject->user_id == $_SESSION['user_session']) :&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <button type="button" class="btn btn-sm btn-outline-secondary">Remove</button>&nbsp; &nbsp; endif;?>大注意事项 我在这段代码中没有看到 a session_start(),因为您正在使用,所以$_SESSION您需要在该脚本的顶部使用其中一个。
打开App,查看更多内容
随时随地看视频慕课网APP