试图显示两个表中的数据

时间:2016-03-01 14:55:40

标签: php mysql

我正在尝试基于另一个表中找到的ID从一个表中$_GET数据。我的第一个表名为user_thoughts,此表保存了用户在我的社交媒体网站上发布的所有公开帖子的数据"。我有另一个名为users的表,它存储了该站点的注册用户的所有详细信息。

我正在尝试在网站的主页上显示所有user_thoughts,但我很难为#34;思想"的作者显示正确的数据。

我们的想法是获取added_by的{​​{1}}(作者),然后使用保存user_thought值的变量并将其与表格中的added_by进行比较username

以下是我的表格以及它的字段:

users

user_thoughts

id message date_of_msg_post time_of_msg_post attachment added_by (只显示相关字段)

users

目标:从id first_name last_name username profile_pic 获取帖子的added_by,然后使用added_by从表格user_thoughts中的用户名字段中获取added_by的详细信息。注意:added_by和username都将保持相同的值。

以下是我尝试过的内容

users

当前行为: 目前,使用上面的代码,它显示所有/* 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. *******/ } ,即表中有三行,每一行都在显示,但是,它的详细信息并非基于这些帖子的作者。例如,写入作者的用户的profile_pic未显示。

2 个答案:

答案 0 :(得分:0)

这会链接用户和user_thoughts。记得要清理你的输入。

* {
  margin: 0 auto;
}
body {
  background-color: beige
}
.wrapper {
  position: fixed;
  background-color: white;
  display: inline-block;
  margin: auto;
  top: 30px;
  left: 30px;
  right: 30px;
}
.header {
  background-color: #3c3c3c;
  margin-bottom: 1em;
  position: fixed;
  right: 30px;
  left: 30px;
}
header {
  text-align: center;
  font-family: Verdana;
  color: White;
  padding: 10px;
}
header h1 {
  font-size: 40px;
}
.hr {
  height: 2px;
  background-color: green;
}
nav {
  text-align: center;
  margin: .1em 0;
}
nav a {
  display: inline-block;
  text-decoration: none;
  font-size: 20px;
  padding: 10px;
  font-family: Arial;
  color: white;
  text-transform: uppercase;
  transition: all .3s;
}
nav a:hover {
  background-color: green;
}
nav #active {
  border-bottom: 2px solid white
}
.main {
  position: fixed;
  z-index: -1;
  right: 30px;
  left: 30px;
  bottom: 30px;
  top: 30px;
  display: inline-block;
  background-color: white;
  overflow: scroll;
}
.content {
  position: static;
  margin-top: 9em;
}

答案 1 :(得分:0)

$query = <<<EOD
    SELECT t.id AS tid, u.id AS uid, * FROM user_thoughts AS t, users AS u
    WHERE t.added_by=u.id ORDER BY t.id DESC
EOD;

$result = mysqli_query($connect, $query);

while ($row = mysqli_fetch_assoc($result)) {
    $thought_id      = $row['tid'];
    $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'];
    $attachment       = $row['attachment'];
    $author_fname    = $row['first_name'];
    $user_profile_dp = $row['profile_pic'];

    // echo your div
}