【问题标题】:How do I make it check if the page is empty with MySQL? [closed]如何使用 MySQL 检查页面是否为空? [关闭]
【发布时间】:2013-02-03 01:14:50
【问题描述】:

我又回来了,尝试创建一个“自定义”博客系统。或者 CMS,正如人们所说的那样。这是我当前的代码:

<?php
//include stuff here
$pid = $_GET['pageid'];
$data = mysql_query("SELECT * FROM entries WHERE id='$pid'") or die("MySQL died.");
mysql_real_escape_string($pid);
while($info = mysql_fetch_array( $data )) 
{
if (!empty($info)) {
echo $info['data'];
}
else {
echo 'This page no existo.';
}
}
?>

发生的情况是它没有显示“此页面不存在”。作为'404'文本。 假设有人试图直接输入我的网站但犯了一个错误: 本地主机/博客/?pageid=10 它不显示 404 文本!

我在 MySQL 中有一个名为“data”的行。它由——嗯……博客文章的数据组成。我还有一个名为 ID 的行,它是一个自动增量 ID 系统。 “真实”的工作页面 ID 是 1。

谢谢, RBLXDev.

编辑: $信息的Vardump: 转储:

array (size=10)
0 => string '1' (length=1)
'id' => string '1' (length=1)
1 => string 'Testing potatoCMS... and the title.' (length=35)
'title' => string 'Testing potatoCMS... and the title.' (length=35)
2 => string 'This is a test.
This is a new line.
This is a cookie.
You are getting fat.
FAT.<br />lol' (length=88)
'data' => string 'This is a test.
This is a new line.
This is a cookie.
You are getting fat.
FAT.<br />lol' (length=88)
3 => string '2013-02-02' (length=10)
'date' => string '2013-02-02' (length=10)
4 => string 'Unspecified' (length=11)
'author' => string 'Unspecified' (length=11)

是的,嗯...我有奇怪的占位符。

【问题讨论】:

  • 尝试在该数组中使用您知道存在的索引,例如:!空($info['id'])
  • 这只是让 pageid=1 “笔记存在”。
  • 也可以考虑proper escaping for SQL context,或者使用更方便的prepared statements
  • 哦,我刚刚发现我有一个SQL注入漏洞。在代码中。
  • 你能在 $info 上做一个 var_dump 吗?

标签: php mysql content-management-system blogs


【解决方案1】:

我试试这样的......

<?php

        $pid = $_GET['pageid'];
        mysql_real_escape_string($pid);

        $data = mysql_query("SELECT * FROM entries WHERE id='$pid'") or die("MySQL died.");

        $num_rows = mysql_num_rows($data);

        if ($num_rows == NULL) {

              echo 'This page no existo.';

        } else {

                  $info = mysql_fetch_array( $data );
                  echo $info['data'];
        }
?>

未测试

更新!!

【讨论】:

  • 成功了!!现在将代码转换为 PDO。
  • 更新了,我没插入$info = mysql_fetch_array( $data );
【解决方案2】:

首先,让我们从你来这里的目的开始:

如果记录不存在,mysql_fetch_array( $data ) 将返回false,因此它甚至不会再进入while 块。所以,你的逻辑是错误的。

其次,您使用 mysql_real_escape_string() 错误。您需要在执行 SQL 查询之前调用它,并且您需要在要注入 SQL 查询的变量中捕获它的输出:

$pid = mysql_real_escape_string($pid);
$data = mysql_query("SELECT * FROM entries WHERE id='$pid'") or die("MySQL died.");

第三,您可能需要考虑完全放弃 mysql_* 函数,因为该库正在被弃用,因为它提供的缓解 SQL 注入的能力很差。考虑使用改进的mysqli_* 库函数或PDO

【讨论】:

    【解决方案3】:
    1. Please, don't use mysql_* functions in new code。它们不再维护and are officially deprecated。看到red box?改为了解 prepared statements,并使用 PDOMySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial

    2. 您必须在在查询中使用数据之前对其进行转义。

    3. mysql_num_rows() 是判断您的查询是否有任何结果的最佳方式。

    4. 如果您只期望查询中的行,则无需遍历所有结果。

    5. 如果 $_GET['pageid'] 始终是一个数字,您应该 cast it to an integer 以减少 SQL 注入的机会

    .

    <?php
    //include stuff here
    $pid = $_GET['pageid'];
    mysql_real_escape_string($pid);
    $data = mysql_query("SELECT * FROM entries WHERE id='$pid'") or die("MySQL died.");
    if (mysql_num_rows() > 0) {
    
        $info = mysql_fetch_array( $data );
        echo $info['data'];
    }
    else {
        echo 'This page no existo.';
    }
    ?>
    

    【讨论】:

    • mysql_real_escape_string 仍然毫无意义。
    • 我的答案使用mysql_num_rows
    • 我知道。稍后我将其更改为 PDO。我会在几分钟后试试这个。
    猜你喜欢
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多