什么是MYSQLI的PHP变量范围

时间:2013-08-08 21:31:47

标签: php mysqli

我正在尝试建立一个简单的评论系统,我想创建评论和登陆页面之间的相关性....所以当用户到达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变量范围的下半部分是?这样我就能确定我们在哪个页面上?这种类型的变量可以通过注释表单中的变量传递吗?

1 个答案:

答案 0 :(得分: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.