BTaylor
BTaylor

Reputation: 11

Why does my code only show one post?

I have been developing a social network, and a key function is to be able to post on other users' profiles. However, my current code will only show one post. Also, it seems to be the first post that is shown. I have tested this by creating new accounts, and writing a test post. The code does show this first post, but if I try it again, only the first post is visible. The code is as follows: The code to send post to database:

$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...";
}

The code to retrieve it (and display it):

$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 />
    ";

If anyone needs some more of the code, like my database connection, just comment.

Upvotes: 0

Views: 134

Answers (1)

dirluca
dirluca

Reputation: 423

In your while cicle you fill in some variables but you do not use them. You use echo only outside the cycle, so just once, and thus you print only the values of the last istance of the while cycle.

Try

    $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 />";
                                                }

Upvotes: 1

Related Questions