我对 php 还很陌生,对于一般的服务器脚本来说(所以对我来说要轻松一点)
但无论如何,我还是成功地创建了这个评论系统的前半部分:
<html>
<body>
<form name="Comment" action="InsertComment.php" method="POST">
Name: <input type="text" name="name" /><br>
Comment: <br><textarea style="height: 100px; width: 600px;" name="comment"></textarea><br>
<input id="Special_ID" name="id" value="<?php $unixtime = time(); echo $unixtime; ?>">
<!--^Gathers a unique id^-->
<input type="submit" />
</form>
</body>
</html>
提交后 -->
<?php
$con = mysql_connect("Blaa", "Blaa", "Blaa");
if(!$con) {
die('Could not connect ' . mysql_error());
}
sql_select_db("Comments", $con);
$sql = "INSERT INTO Posts (Name, Comment, ID)
VALUES('$_POST[name]', '$_POST[comment]', '$_POST[id]')";
?>
这正是我想要的,用户输入他们的名字,评论,并生成一个唯一的帖子ID(时间戳),然后将其全部发送到mysql。
但现在我很困惑如何将其发布到另一个页面..
我假设是这样的:
if(ID == [the id of that post]) {
//$_GET the mysql stuff
//Post inside a specially made div or something
}
沿着这个思路,但我不知道如何将其付诸实践:/ 有任何想法吗? 哦,请不要建议 echo 类型的帖子,我已经这样做了,但这根本不是我想要的。
**另外,这只是基本代码,我还不需要有关如何修改它的建议,而且其中的错误只是由于我的 sleep 不足,代码确实有效。
最佳答案
正如 @Marc B 所说,你首先要修复你的 SQL injection使用 mysql_real_escape_string 打洞。将插入语句更改为
$sql = "INSERT INTO Posts (Name, Comment, ID)
VALUES('" . mysql_real_escape_string($_POST['name']) . "', '" . mysql_real_escape_string($_POST['comment']) . "', '" . mysql_real_escape_string($_POST['id']) . "')";
要显示您的评论,请尝试此操作
$sql = "SELECT Name, Comment, ID
FROM Posts
WHERE ID = '" . mysql_real_escape_string($_GET['PostID']) . "'";
$query = mysql_query($sql);
echo "<div id=\"comments_container\">";
while ($row = mysql_fetch_assoc($query))
{
echo "<div class=\"comment\">";
echo "<div class=\"name\">" . $row['Name'] . "</div>";
echo "<div class=\"comment_body\">" . $row['Comment'] . "</div>";
echo "</div>"
}
echo "</div>";
然后CSS style您的 DIV 使用 IDs and classes .
关于php+mysql评论系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8138760/