我正在編寫帶有評論的新聞更新系統。我有三張桌子。如何從GROUP_CONCAT中刪除單個行?
news_tbl
users_tbl
(新聞帖子的作者)comments_tbl
(與外鍵連接 - news_id)
我已經嘗試了兩種方法,且無GROUP_CONCAT
。
我已經試過這樣:
SELECT *, COUNT(comments_tbl.comments_id) AS `comments_count`
FROM news_tbl AS n
LEFT JOIN users_tbl AS u
ON n.user = u.username
LEFT JOIN comments_tbl AS c
ON c.news_id = n.news_id
GROUP BY n.news_id;
它的工作原理,但我只能顯示第一書面意見,但是,我想顯示所有的意見,貼在新聞更新。
我也使用GROUP_CONCAT
作爲評論,所以每個評論都不會有新聞發佈的重複。我不確定這是否是正確的方法,但它有效。
我的問題是,我有一個登錄系統,我希望用戶能夠單獨刪除註釋,併爲每個註釋添加一個刪除按鈕。但我不知道如何單獨分開評論,並從每條評論中獲取該ID。我嘗試過使用PHP的explode
,但它並不適合我。
這是我的代碼:
<?php
require_once('connect.inc.php');
$conn = dbConnect('pdo') or die('no connection');
$sqlquery = "SELECT news_tbl.*, users_tbl.*,
GROUP_CONCAT(comments_tbl.comments_id) AS commentsid,
GROUP_CONCAT(CONCAT ('<hr><h6>Navn: ', comments_tbl.name,'</h6>','<p>',comments_tbl.comment, '</p>') SEPARATOR '<br><br>') AS comments,
COUNT(comments_tbl.comments_id) AS comments_count
FROM news_tbl
LEFT JOIN comments_tbl ON comments_tbl.news_id = news_tbl.news_id
LEFT JOIN users_tbl ON news_tbl.user = users_tbl.username
GROUP BY news_tbl.news_id
ORDER BY news_tbl.news_id DESC LIMIT 5";
$result = $conn->query($sqlquery);
foreach($result as $row) {
// AS
$commentsid = $row['commentsid'];
$comments = $row['comments'];
$count = $row['comments_count'];
//users_tbl
$username = $row['username'];
//news_tbl
$newsid = $row['news_id'];
$headline = $row['headline'];
$bodytext = $row['bodytext'];
$picture = $row['picture'];
$date = $row['date'];
//comments_tbl
$commentid = $row['comments_id'];
$name = $row['name'];
$comment = $row['comment'];
?>
<div class="row-fluid">
<div class="news">
<div class="text-left">
<h3 class="pull-top"><?php echo $headline ?></h3>
<div class="row-fluid span12">
<img src="<?php echo 'upload/'.$picture ?>" class="span9 img-news"/>
</div>
<div class="row-fluid">
<div class="span3">
<p class="author"><i><span class="red">Skrevet af: </span> </i><?php echo $username ?></p>
</div>
<div class="span4">
<p><i ><span class="red">Oprettet:</span> </i> <?php echo $date ?></p>
</div>
</div><!--/ End author-row -->
<!--<p class="author"><i><span class="red">Skrevet af: </span> </i><?php echo $username ?> | <i ><span class="red">Oprettet:</span> </i> <?php echo $date ?></p>-->
<p class="push-top-bottom"><?php echo $bodytext ?></p>
</div>
<hr>
<p class="text-left pull-top3"> <b><?php echo $count ?> kommentar</b></p>
<hr class="grey">
<div class="row-fluid pull-top2">
<div class="pull-left span12 text-left well">
<?php echo $comments; ?>
</div>
<!-- Opret kommentar -->
<div class="row-fluid pull-left">
<form action="insert.php?<?php echo 'news_id=' . $newsid; ?>" method="post" class="pull-left leave-comment">
<input type="text" name="name" id="name" class="comment" placeholder="Navn" required/> <br> <br>
<textarea type="text" name="comment" id="comment" class="comment" placeholder="Skriv kommentar" rows="5" required></textarea> <br> <br>
<input type="submit" value="Opret kommentar" class="btn pull-left"/>
</form>
</div>
</div><!--/ End comments-row -->
</div><!--/ End news-container -->
</div><!--/ End news row -->
<?php
if (isset($_SESSION['valid_user'])) {
// onClick slet nyhed med alert box
echo '<div class="row-fluid">';
echo ' <div class="pull-left">';
echo ' <a class="btn btn-danger" onclick="deleteNews('. $newsid.')" style="margin-right:20px;">Slet nyhed</a>';
echo ' <a href=update.php?news_id='.$newsid.'&username='. $username .' class="btn btn-info">Updater nyhed</a>';
echo ' <br><br><br><br>';
echo ' </div>';
echo '</div>';
}
?>
<hr>
<?php
//End foreach
}
?>
我不會在這裏使用一個組,而只是獲取單個記錄。但是,如果你想這樣:爲什麼不做一個單獨的'GROUP_CONCAT(comments_tbl.comments_id)'? – Wrikken
那麼,我該怎麼做呢,沒有GROUP_CONCAT?我嘗試了不同的JOINS,但是如果沒有GROUP_CONCAT,我就無法工作,所以我可以顯示每條新聞和附加的所有評論,我只會收到一條評論,或者重複,而且我還沒有獲得單獨的ID評論 - 刪除它們。 – user1906437