大家好,所以我现在为我的网站创建了一个简单的评论框。它运行良好,但是我遇到的问题是我有不同的页面,需要不同的评论框。我似乎无法弄清楚如何让评论框对每个页面都是唯一的。所以现在我的数据库保存着这个:
Called comments:
id
comment
comment1
comment_date
现在我的想法是所有内容都存储在评论中,所以我为其他页面添加了 comment1 来存储信息。但是我不知道如何编辑 php 文件以使其与 comment1 一起使用。对此的任何帮助都会很棒。
.HTML:
<div class="comment_container">
<div class="comments">
<?php
include_once("comments.php");
?>
</div>
<div class="comments_form">
<table>
<tr><td><textarea id="comment_text"></textarea></td>
<td><input type="button" id="comment_process" value="Post Comment"/></td></tr>
</table>
</div>
</div>
.JS:
$(document).ready(function() {
$('#comment_process').click(function() {
if ($('#comment_text').val() != "") {
$.post("comments.php?action=post", {
comment: $('#comment_text').val()
}, function(data) {
$('.comments').html(data);
$('#comment_text').val("");
});
}
});
});
.PHP:
include_once("connect.php");
function convert ($date) {
$converteddate = date("F j, Y g:ia", strtotime($date." +1day"));
return $converteddate;
}
function getComments(){
$comments = "";
$sql = mysql_query("SELECT * FROM comments") or die(mysql_error());
if(mysql_num_rows($sql) == 0){
$comments = "<div class='each_comment'>There are no comments</div>";
} else {
while($row = mysql_fetch_assoc($sql)){
$comments .= "<div class='each_comment'><small><em>".convert($row['comment_date'])."</em></small><br />".$row['comment']."</div>";
}
}
return $comments;
}
function postComments($comment){
$comment = mysql_real_escape_string(strip_tags($comment));
$sql = mysql_query("INSERT INTO comments (comment, comment_date ) VALUES ('".$comment."', now())");
return true;
}
if((isset($_GET['action'])) && ($_GET['action']== "post")){
postComments($_POST['comment']);
}
echo getComments();
再次感谢您的帮助
免
责声明
对于未来的访客:不要复制此代码,因为它有几个问题超出了回答问题的范围。
您需要添加的是注释类型的标识器。(类型可以替换为更适合您的情况的内容,例如"产品","用户",...无论区别是什么/它们与什么有关(
因此,在您的数据库中添加新列:
comments
--------
id
comment
type
comment_date
现在你需要通过所有的调用传递这个类型,它应该在你的"HTML"页面中指定(实际上是php...(。
<div class="comment_container">
<div class="comments">
<?php
// specify the type needed on that page
$type = 1;
include_once("comments.php");
echo getComments($type);
?>
</div>
<div class="comments_form">
<table>
<tr><td><textarea id="comment_text"></textarea></td>
<td><input type="button" id="comment_process" value="Post Comment"/></td></tr>
</table>
</div>
</div>
<script>
// specify the type in javascript
var type=1;
$(document).ready(function() {
$('#comment_process').click(function() {
if ($('#comment_text').val() != "") {
// add the type here:
$.post("comments.php", {
comment: $('#comment_text').val(),
type: type,
action: 'post'
}, function(data) {
$('.comments').html(data);
$('#comment_text').val("");
});
}
});
});
</script>
并在评论中.php:
//....some code left out here
function getComments($type){
$comments = "";
$sql = mysql_query("SELECT * FROM comments where type=$type") or die(mysql_error());
if(mysql_num_rows($sql) == 0){
$comments = "<div class='each_comment'>There are no comments</div>";
} else {
while($row = mysql_fetch_assoc($sql)){
$comments .= "<div class='each_comment'><small><em>".convert($row['comment_date'])."</em></small><br />".$row['comment']."</div>";
}
}
return $comments;
}
function postComments($comment, $type){
$comment = mysql_real_escape_string(strip_tags($comment));
$sql = mysql_query("INSERT INTO comments (comment, comment_date, type ) VALUES ('".$comment."', now(), ".$type.")");
return true;
}
if((isset($_POST['action'])) && ($_POST['action']== "post")){
postComments($_POST['comment'], $_POST['type']);
// send all the comments back to client
echo getComments($_POST['type']);
}
// moved to html-file: echo getComments($type);
注意
该代码存在几个问题。
首先不要使用 mysql 函数。是真的。不安全且自 php7 起已弃用/删除。使用 mysqli 或 pdo。此外,你的sql可以被sql注入所破解。阅读准备好的语句。
该代码的总体结构不是很好。尝试将输出和格式与获取数据分开。例如,如果一个名为"getComments"的函数只从数据库中获取注释,然后让其他人决定如何处理这些数据,那就更好了。一个函数做得越少越好。
请阅读编码风格,也许开始学习面向对象编程。
我希望这仍然可以帮助您了解要去哪里!