1

我正在尝试建立一个简单的评论系统,并且我想创建评论和登陆页面之间的关联......所以当用户到达 blog.php?id=3 时,他们会看到正确的评论。

我正在做的是创建带有 pageid 列的评论表。当用户发布到页面时,pageid 列将被填充。也许是一个隐藏的表单域?如何在我的 MYSQLI 中建立这种关联

这就是我在想的...

<?php
include_once("includes/check_login_status.php");
?>
<?php
// Check to see the URL variable is set and that it exists in the database
if (isset($_GET['id'])) {
// Connect to the MySQL database
include "includes/db_conx.php";
$id = preg_replace('#[^0-9]#i', '', $_GET['id']); // filter everything but numbers
// Use this var to check to see if this ID exists, if yes then get the product
// details, if no then exit this script and give message why


$sql = "UPDATE content SET views=views+1 WHERE ID=$id";
$update = mysqli_query($db_conx,$sql);

$sql = "SELECT * FROM content WHERE id=$id LIMIT 1";
$result = mysqli_query($db_conx,$sql);
$productCount = mysqli_num_rows($result);

if ($productCount > 0) {
// get all the product details
while($row = mysqli_fetch_array($result)){
$article_title = $row["article_title"];
$category = $row["category"];
$readmore = $row["readmore"];
$author = $row["author"];
$date_added = $row["date_added"];
$article_content = $row["content"];
}

} else {
echo "That item does not exist.";
exit();
}

} else {
echo "Data to render this page is missing.";
exit();
}

?>

<?php 
include_once "includes/db_conx.php";

$sql = "SELECT * FROM comment WHERE pageid ="$id"ORDER BY id DESC";

$sql_comments = mysqli_query($db_conx,$sql);
while($row = mysqli_fetch_array($sql_comments)){
 $name = $row["name"];
 $comment = $row["comment"];

 $commentlist .= 'name : '.$name.'<br />comment : '.$comment.'<hr>';
}
//////////////
?>

下半部分是否在 get 变量的范围内?这样我就可以确定我们在哪个页面上?这种类型的变量可以通过注释表单中的变量传递吗?

4

1 回答 1

0

第 3 条 sql 语句包含错误:

$sql = "SELECT * FROM comment WHERE pageid ="$id"ORDER BY id DESC";

$sql = "SELECT * FROM comment WHERE pageid =".$id."ORDER BY id DESC";

您可能还想将 pre_replace 语句更改为 intval:

$id = preg_replace('#[^0-9]#i', '', $_GET['id']);

$id = intval($_GET['id']);

原因是如果 $_GET['id'] = 'ABC123' 那么 preg_replace 将返回 123 而 intval 将返回 0。

于 2013-08-08T21:45:08.153 回答