我想问一下,
我有两个表,用户和帖子与列字段
用户:user_id,姓名,电子邮件
帖子:post_id,user_id,post_title
我想显示所有用户的所有帖子,
但我只希望 logged_in用户会话再增加两个按钮,而其他公开帖子只有两个按钮
p / s:我在users表中使用电子邮件列字段作为登录 $ _ SESSION 。
<?php
global $connect;
global $user_id;
$sql_post = "SELECT * FROM posts";
$run_post = mysqli_query($connect, $sql_post);
if($run_post && mysqli_num_rows($run_post) > 0 )
{
while($row_post = mysqli_fetch_array($run_post))
{
$post_id = $row_post['post_id'];
$user_id = $row_post['user_id'];
$post_title = $row_post['post_title'];
$sql_user = "SELECT * FROM users WHERE user_id='$user_id'";
$run_user = mysqli_query($connect, $sql_user);
$check_user = mysqli_fetch_array($run_user);
$user_id = $check_user['user_id'];
$user_name = $check_user['name'];
$user_email = $check_user['email'];
$post_output = "<div id='posts_wrap'>
<p>$user_name</p>
<p>$user_email</p>
<p>$post_title</p>
<a href=''><button>Like</button></a>
<a href=''><button>Comment</button></a>
// i want these two button (Edit and Delete) only available to logged in user
<a href=''><button>Edit</button></a>
<a href=''><button>Delete</button></a>
</div>
";
echo $post_output;
}
mysqli_free_result($run_post);
}
else
{
echo "No post yet";
}
?>
答案 0 :(得分:0)
用户登录后,在会话中保留用户详细信息,如果用户登录则检查条件。例如,如果您尝试评论并且仅对登录用户感兴趣那么您可以做一些事情,如
<?php
session_start();
$_SESSION['email']='email@example.com';
$user_name='dd';
$user_email='ddd';
$post_title='gsdg';
$post_output = "<div id='posts_wrap'><p>$user_name</p><p>$user_email</p><p>$post_title</p>";
if(isset($_SESSION['email'])){
$post_output.="<a href=''><button>Like</button></a><a href=''><button>Comment</button></a> ";
}
// i want these two button (Edit and Delete) only available to logged in user
$post_output.= "<a href=''><button>Edit</button></a><a href=''><button>Delete</button></a> </div>";
print_r($post_output);
?>
在上面的代码用户登录,所以所有按钮都是可见的。如果没有那么它对所有人都不可见。只是试图破坏session.i认为以前的会话电子邮件仍在那里
答案 1 :(得分:-1)
我找到了解决方案。事实证明,我需要创建另一个查询,结合user_id和电子邮件,以确保登录属于登录用户。这是代码
<?php
global $connect;
global $user_id;
$get_post = "SELECT * FROM posts";
$run_post = mysqli_query($connect, $get_post);
if($run_post && mysqli_num_rows($run_post) > 0 )
{
while($row_post = mysqli_fetch_array($run_post))
{
$post_id = $row_post['post_id'];
$user_id = $row_post['user_id'];
$post_title = $row_post['post_title'];
$emailsql = $_SESSION['email'];
$get_email = "SELECT * FROM users WHERE user_id='$user_id' AND email='$emailsql'";
$run_email = mysqli_query($connect, $get_email);
$check_email = mysqli_fetch_array($run_email);
$d_email = $check_email['email'];
$get_user = "SELECT * FROM users WHERE user_id='$user_id'";
$run_user = mysqli_query($connect, $get_user);
$check_user = mysqli_fetch_array($run_user);
$user_id = $check_user['user_id'];
$user_name = $check_user['name'];
echo "<div id='posts_wrap'>
<p><h3><a href='userprofile.php?user_id=$user_id'>$user_name</a></h3></p>
<div id='posts_title'>
<p><h3><a href='post.php?post_id=$post_id'>$post_title</a></h3></p>
</div>
<hr>
<a href=''><button>Like</button></a>
<a href=''><button>Comment</button></a>
";
if($check_email){
echo "
<a href=''><button>Edit</button></a>
<a href=''><button>Delete</button></a>";
}
echo "</div>";
}
mysqli_free_result($run_post);
}
else
{
echo "No post yet";
}
?>