以下代码从表 CHART 和 COMMENTS 中从 MySQL 数据库中获取带有注释的消息(状态)。当它从数据库返回导致评论字段和消息没有出现在 HTML 页面中的消息时,代码的问题正在消失,因为 msg_id 与表 COMMETS 中的任何内容都不匹配。
有人可以帮我解决这个问题吗?或者我能做到这一点的更好方法是什么?
示例代码注意:首先我有一个表单用于创建消息,然后是波纹管代码来获取我要评论的消息并返回已评论消息下方的评论。
<?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>\n";
$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>";
}
?>