非常抱歉,因为这将是一个很长的帖子。我是MYSQL的初学者,我刚开始学习。 我试图建立用户评论回复系统。我完全卡住了。
我有一个带有表名的数据库' post'用以下结构的评论
TABLE `post` (
`id_posta` int(4) NOT NULL,
`tekst_posta` text NOT NULL,
`name` text NOT NULL,
`slika_posta` blob NOT NULL,
`type` text NOT NULL,
`vreme_posta` datetime NOT NULL,
`id_autora` int(4) NOT NULL,
`ime` varchar(50) NOT NULL,
`prezime` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

和表名' komentar',其中书面回复评论
TABLE `komentar` (
`id_komentara` int(11) NOT NULL,
`tekst_komentara` text NOT NULL,
`id_aut_komentara` int(4) NOT NULL,
`id_autora` int(4) NOT NULL,
`vreme_komentara` datetime NOT NULL,
`id_posta` int(4) NOT NULL,
`imek` varchar(50) NOT NULL,
`prezimek` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin

当我写下所有评论时,一切都很完美。
$upit = "SELECT id_posta, slika_posta, vreme_posta, tekst_posta, id_autora, ime, prezime FROM post ORDER BY `post`.`id_posta` DESC;";
$postovi = $con->query($upit);
if($postovi->num_rows > 0)
{
while($red = $postovi->fetch_assoc())
{
$idp = $red["id_posta"];
echo "<table border-collapse: separate; empty-cells: hide; class='my-special-table'>";
echo "<tr><td>";
echo $red["ime"]. " " . $red["prezime"]. ':' ;
echo "</td></tr>";
echo "<tr><td class='prvatd'>";
echo $red["tekst_posta"];
echo "</td></tr>";
if( $red["slika_posta"] != '' ){
echo '<tr><td class="w_imgg"><center><img src="data:image/png;base64,'.base64_encode($red["slika_posta"]).'" class="w_img"></center></td></tr>';
}
echo "<br>";
echo "<tr><td style= 'color:#FF0000; font-size:12px;'>";
echo 'Objavljeno' . ' ' . $red["vreme_posta"];
echo "</td></tr>";
echo "<tr><td>";
echo "<form method='post' action = '' >
<textarea id = 'styled' name='tekst_komentara' placeholder='Prokomentarisite'></textarea><br>
<input type='hidden' name='id_pos' value= $idp/>
<input type='submit' name='unesi_komentar' value='Objavi komentar'/>
</form>";
echo "</td></tr>";
echo "<tr><td>";
echo REPLY ?????
echo "<td></td>";
echo "<tr><td>Lajk<td></td>";
echo "</table>";
echo "<br><br>";
}
}
$con->close();
&#13;
当我尝试在回复td中回复相应评论时,问题就出现了。
我试试这个......
$con = new mysqli ("localhost", "root", "", "introduce");
$upit = "SELECT id_posta, slika_posta, vreme_posta, tekst_posta, id_autora, ime, prezime FROM post ORDER BY `post`.`id_posta` DESC;";
$postovi = mysqli_query($con,$upit);
if($postovi->num_rows > 0)
{
while($red = $postovi->fetch_assoc()) {
echo "<table>";
echo "<tr><td>";
echo $red["tekst_posta"];
echo "</td></tr>";
echo "</table>";
$upit1 = "SELECT * FROM komentar INNER JOIN post ON post.id_posta=komentar.id_posta ;";
$komentari = mysqli_query($con,$upit1);
while($red = $komentari->fetch_assoc()) {
echo "<table>";
echo "<tr><td>";
echo $red["tekst_komentara"];
echo "</td></tr>";
echo "</table>";
}
}
}
&#13;
,结果是
2 2 (second post)
1-1 2-1 (first reply)
1-2 2-2 (second reply)
2-1
1 and should be 1 (first post)
1-1 1-1 etc,etc....
1-2 1-2
2-1
&#13;
然后我试试这个
$con = new mysqli("localhost", "root", "", "introduce");
$sql = "SELECT * FROM komentar INNER JOIN post ON post.id_posta=komentar.id_posta ; ";
$komentari = $con->query($sql);
if($komentari->num_rows > 0)
{
while($red = $komentari->fetch_assoc())
{
echo $red["tekst_posta"];
echo "<br>";
echo $red["tekst_komentara"];
}
}
&#13;
结果是
1
1-1
1
1-2
2
2-1 duplicate post on every replay....
2
2-2
2
2-3
&#13;
接下来我试试......
$sql = "SELECT id_posta, slika_posta, vreme_posta, tekst_posta, id_autora, ime, prezime FROM post ORDER BY `post`.`id_posta` DESC;";
$sql .= "SELECT * FROM post JOIN SELECT * FROM komentar WHERE komentar.id_posta = 'post.id_posta'";
&#13;
我尝试GROUP BY并尝试,并尝试.....
我正在考虑阵列....所以,我请求你的建议,因为我将永远无法做到这一点。