【问题标题】:Query not being added to SQL Database using PHP未使用 PHP 将查询添加到 SQL 数据库
【发布时间】:2014-01-02 05:05:13
【问题描述】:

查询未在使用 PHP 的 SQL 数据库上运行。输出是这样说的,但是当我检查它时,它不存在。请帮忙! Btw 'connectdb.php' 连接到 sql 和数据库。有什么我忘记的功能吗?

<?php
$userId = mysql_real_escape_string($_GET["UserId"]);
$banner = mysql_real_escape_string($_GET["banner"]);
include "connectdb.php";

$query = mysql_query("SELECT * FROM banned WHERE Userid='$userId'");
$numrows = mysql_num_rows($query);
if (numrows == 1) {
    echo "true";
} else {
    echo "false";
}
?>

【问题讨论】:

    标签: php mysql sql get


    【解决方案1】:

    直接回答问题:

    您应该在调试时将以下内容添加到查询行中,如下所示,以获得错误输出:

    or die(mysql_error());
    

    抛开这一点,考虑使用 PDO,你会发现它的错误信息非常冗长,也可以让你不必运行 mysql_real_escape_string(),并提供更多的 SQL 注入保护。

    【讨论】:

    • 我在 if 语句中的 numrows 中添加了现金标记,并添加了 mysql 查询。仍然没有添加到它。
    • 如果你运行这个,输出是什么?你有打开错误显示吗?$query = mysql_query("SELECT * FROMbanned WHERE Userid='$userId'")or die(mysql_error());
    【解决方案2】:

    您在 if 条件中忘记了 $ 可能这就是您的代码无法正常工作的原因

    <?php
    
    include "connectdb.php";
    
    $userId = mysql_real_escape_string($_GET["UserId"]);
    $banner = mysql_real_escape_string($_GET["banner"]);
    
    $query = mysql_query("SELECT * FROM banned WHERE Userid='$userId'");
    $numrows = mysql_num_rows($query);
    if ($numrows > 1) {
        echo "true";
    } else {
        echo "false";
    }
    ?>
    

    如果banned 表中有一列Userid 字段为$userId,则上述代码将返回true,否则将返回false

    【讨论】:

    • 您是否在 UserId 中获得了值?
    • 是的,这是输出:“UserId: 232 被用户禁止”
    • 如果 $userId 存在于您的“禁止”表中,这应该是 true
    • 为什么要删除转义调用并使查询容易受到 SQL 注入攻击?
    【解决方案3】:

    您在 if 语句中的 numrows 之前缺少 $ 。

    【讨论】:

      【解决方案4】:

      将包含链接放在文件顶部

      include "connectdb.php";
      $userId = mysql_real_escape_string($_GET["UserId"]);
      $banner = mysql_real_escape_string($_GET["banner"]);
      

      然后将 $ 添加到 numrows

      if ($numrows == 1) {
      

      你也可以使用

      if ($numrows > 0) {
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多