我试图根据另一个表中找到的id从一个表中获取$_GET
数据。我的第一张表叫做user_thoughts
,这张表包含用户在我的“社交媒体网站”上发布的所有公共帖子的数据。我有另一个表users
,它存储了该网站的注册用户的所有细节。试图显示两个表中的数据
我想在网站的主页上显示所有的user_thoughts
,但我努力为“思想”的作者展示正确的数据。
的想法是让added_by
(作者)的user_thought
的然后使用变量保存的added_by
的价值,并将其与表users
发现username
。
这里是我的表的,它的领域:
user_thoughts
:
id
message
date_of_msg_post
time_of_msg_post
attachment
added_by
users
(只显示相关领域)
id
first_name
last_name
username
profile_pic
目的:获取ŧ他添加了user_thoughts
的帖子,然后使用added_by从表users
的用户名字段中获取added_by的详细信息。注意:added_by和username都会保持相同的值。
这是我曾尝试:
/* How it works: The id of each user_thought will be used to determine which user posted it.
* Then display their details accourdingly.
*/
$get_thoughts_from_db = mysqli_query($connect, "SELECT * FROM user_thoughts ORDER BY id DESC"); // newest posts first
while ($row = mysqli_fetch_assoc($get_thoughts_from_db)) {
$thought_id = $row['id'];
$msg_content = $row['message'];
$date_of_msg = $row['date_of_msg_post'];
$thoughts_by = $row['added_by'];
$time_of_msg = $row['time_of_msg_post'];
$attachent = $row['attachment'];
$get_user = $_GET['id'];
} // while closed
// Get the details of the user based on the ID of the user thought.
$get_data = mysqli_query($connect, "SELECT * FROM users WHERE id = '$get_user'");
$get_user_data = mysqli_fetch_assoc($get_data);
$author_fname = $get_user_data['first_name'];
$user_profile_dp = $get_user_data['profile_pic'];
// displaying all the posts in the database on the main page.
// Will limit 15 posts per page, and will order them by the date and time posted (latest posts first)
$get_all_posts_q = mysqli_query ($connect, "SELECT * FROM user_thoughts ORDER BY id DESC ");
$check_rows = mysqli_num_rows($get_all_posts_q);
while ($get_row = mysqli_fetch_array($get_all_posts_q)){
$message = $get_row['message'];
/**** Between the while loop is where I echo the div(s)
which display the above details. *******/
}
当前的行为: 目前,与上面的代码,它会显示所有user_thoughts
,即有三个表中的行,并显示每一个,但是,它的细节不是基于这些职位的作者。例如,撰写作者的用户的profile_pic未显示。
你很容易受到[SQL注入攻击(http://bobby-tables.com)。并且请注意,您正在循环中获取您的user_thoughts结果,但在循环完成之后才会使用这些结果,因此您只能使用您提取的LAST思想。 –
您是试图显示来自单个用户的想法列表,还是显示所有用户的想法列表? – OscarJ
@OscarJ - user_thoughts表中的每一行都应显示在主页上。所以要回答你的问题,所有的用户。 – Freddy