为什么我的代码只显示一个帖子?

时间:2014-03-07 20:49:07

标签: php html database post forum

我一直在开发社交网络,关键功能是能够发布其他用户的个人资料。但是,我当前的代码只显示一个帖子。此外,它似乎是第一个显示的帖子。我通过创建新帐户和编写测试帖来测试这一点。代码确实显示了第一篇文章,但如果我再次尝试,只有第一篇文章可见。代码如下: 发送到数据库的代码:

$person = "profile.php?id={$id}";
$post = $_POST['post'];
if($post != "")
{
    $data_added = date("Y-m-d");
    $added_by = $session_username;
    $user_posted_to = $id;
    $post = preg_replace("#[^0-9a-z]#i", "", $post);
    $sqlCommand = "INSERT INTO posts VALUES ('', 
                                             '$post', 
                                             '$data_added', 
                                             '$added_by', 
                                             '$user_posted_to')";
    $commandQuery = mysql_query($sqlCommand) or die ("Couldn't send post");
}
else
{
    echo "You have to fill in the post form...";
}

检索它(并显示它)的代码:

$getPosts = mysql_query("SELECT * 
                           FROM posts 
                          WHERE user_posted_to='$id' 
                       ORDER BY id DESC LIMIT 15") or die("Couldn't find any posts");
while($row = mysql_fetch_array($getPosts))
{
    $id = $row['id'];
    $body = $row['body'];
    $date_added = $row['date_added'];
    $added_by = $row['added_by'];
    $user_posted_to = $row['user_posted_to'];
    $querya = mysql_query("SELECT * 
                             FROM members 
                            WHERE username='$added_by' LIMIT 1");
    while($row = mysql_fetch_array($querya))
    {
        $user_added = $row['id'];                           
    }
    $user_added = "profile.php?id={$user_added}";
}

echo "
    <div>
    <h3><a href='$user_added'>$added_by</a> - $date_added </h3>
    <p> $body</p>
    </div><br />
    ";

如果有人需要更多代码,比如我的数据库连接,只需注释。

1 个答案:

答案 0 :(得分:1)

在你的同时,你填写一些变量,但你不使用它们。 您只在循环之外使用echo,因此只打印一次,因此您只打印while周期的最后一个距离的值。

尝试

    $getPosts = mysql_query("SELECT * FROM posts WHERE user_posted_to='$id' ORDER BY id DESC LIMIT 15") or die("Couldn't find any posts");
                while($row = mysql_fetch_array($getPosts)){
                    $id = $row['id'];
                    $body = $row['body'];
                    $date_added = $row['date_added'];
                    $added_by = $row['added_by'];
                    $user_posted_to = $row['user_posted_to'];

                    $querya = mysql_query("SELECT * FROM members WHERE username='$added_by' LIMIT 1");
                    while($row = mysql_fetch_array($querya)){
                        $user_added = $row['id'];                           
                        }
                        $user_added = "profile.php?id={$user_added}";
                    echo "
                    <div><h3><a href='$user_added'>$added_by</a> - $date_added </h3><p> $body</p></div><br />";
                                                }