Hey guys, I am coding some sort of a "clone" of Twitter. Right now I'm coding the status updates, bit I have a problem. I wanted to add a comment function to the statuses, and I have made the statuses display with no problem, and i made a commenter that add's the status_id in to the database. Well, my problem is that when It is running a mysql_query it chooses another status id then the one i wanted to use, so the comment that I post displays on another status. I think I know what the problem is, but I can't seem to figure out how to fix it. I think the problem is that I am running a mysql query that displays more then just the one status that i want to comment on, and it chooses the latest status instead of the one I picked.
Here is my source, all help is appreciated! 🙂
status_updates.php
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="../js/showhide.js"></script>
</head>
<font color="#999999" size="+2"><u>Statusoppdateringer</u></font>
<br />
<br />
<div style="width:500px">
<?php
include '../includes/database.php';
$query = mysql_query("SELECT * from status_updates ORDER BY id DESC LIMIT 8") or die (mysql_error());
while ($row = mysql_fetch_array($query))
{
$userid = $row['userid'];
$query2 = mysql_query("SELECT * from users WHERE id='$userid'");
while ($row2 = mysql_fetch_array($query2))
{
$status_id = $row['id'];
$result = mysql_query("SELECT * FROM status_comments WHERE commented_status_id='$status_id'");
$num_rows = mysql_num_rows($result);
echo '<a href="profile.php?profile=' . $row2['username'] . '"><b><img src="' . $row2['picture_thumb'] . '" width="30"><font color="black" style="margin-left:2px">' . ucfirst($row2['fullname']) . '</font></b></a>';
echo '<br />';
$_SESSION['status_id'] = $status_id;
echo $row['status'];
echo '
<div>
<h4 style="margin:0"><a href="#" onclick="showContent(this);">' . $num_rows . ' ';
if ($num_rows==0)
{
echo 'Kommentarer';
}
else
{}
if ($num_rows==1)
{
echo 'Kommentar';
}
else
{}
if ($num_rows>1)
{
echo 'Kommentarer';
}
else
{}
echo '
</a></h4>
<div style="display:none">
<!--Dropdown Comment DIV-->
';
$query3 = mysql_query("SELECT * from status_comments WHERE commented_status_id='$status_id'");
while ($row3 = mysql_fetch_array($query3))
{
echo $row3['comment'];
echo '<br/>';
}
echo '<form action="post_comment.php" method="POST" /><br />
Comment: <input type="text" name="comment" />
<br />
<input type="submit" name="submit" value="Post kommentar!" />
</form>
<!--Dropdown Comment DIV - END-->
</div>
</div>';
echo '<br />';
}
}
?>
</div>
post_comment.php
<?php
session_start();
include '../includes/database.php';
$username = strtolower($_SESSION['username']);
if (isset($_POST['submit']))
{
$comment=$_POST['comment'];
$status_id=$_SESSION['status_id'];
mysql_query("INSERT INTO status_comments (comment, commenter, commented_status_id) VALUES ('$comment', '$username', '$status_id')") or die(mysql_error());
echo '<script>history.back(1);</script>';
}
else
{
echo "Du kan ikke nå denne siden uten å poste en kommentar.";
}
?>