The following code is fetching messages (status) with its comments from MySQL database from tables CHART and COMMENTS. The problem with the code is dieing when it's returning the messages from database causing the comment field and messages not appear in an HTML page because the msg_id didn't match anything in the table COMMETS.
Can someone help me to solve this? or what is the better way i can do this?
sample code Note: First i have a form am using to create messages then the bellow code to fetch my messages to be commented and return the comments bellow the message which has been commented.
<?php
//MYQSL query to select from two tables chart and comments
//this query is not returning anything because the script is dieing ON ch.msg_id=co.comment_id, it cant find co.comment_id because has not yet created.
$result = mysqli_query($con, "SELECT ch.msg , ch.msg_id , co.comment , co.comment_id
FROM chart AS ch
JOIN comments AS co
ON ch.msg_id=co.comment_id
ORDER BY ch.msg_id, co.comment_id");
$last_msg = null;
while($row = mysqli_fetch_array($result))
{
if ($row['msg_id'] !== $last_msg) {
if ($last_msg !== null) {
echo "<table>";
}
echo "<table border='1' width='600px'>
<tr>
<td>
$row[msg]
</td>
</tr>
";
$last_msg = $row['msg_id'];
}
//all comments should appear here if comment_id match msg_id in COMMENTS table
echo("
<tr>
<td>
$row[comment]
<p></p>
//HTML form which will insert the comments into table COMMENTS
//this form should appear below each message since its a comment field
<form action='drop_comment.php' method='post'>
<input type='text' name='comment' placeholder='drop a comment...' value='' class='add_hook'>
<input name='comment_id' type='hidden' value='$row[msg_id]'>
</form>
</td>
</tr>
");
echo "</table>";
}
?>