0

我使用下面的代码来确保只有确认的用户(确认为 1,否则为 0)可以登录,但无论如何此代码都会显示道歉消息。我怎样才能解决这个问题?谢谢!

// make sure the user is confirmed
    $confirmstatus = query("SELECT * FROM users WHERE username = ?", $_POST["username"]);
    if ($confirmstatus["confirmed"] == 0)
    {
        apologize("Please first confirm your account.");
    }

这是查询功能

    /**
 * Executes SQL statement, possibly with parameters, returning
 * an array of all rows in result set or false on (non-fatal) error.
 */
function query(/* $sql [, ... ] */)
{
    // SQL statement
    $sql = func_get_arg(0);

    // parameters, if any
    $parameters = array_slice(func_get_args(), 1);

    // try to connect to database
    static $handle;
    if (!isset($handle))
    {
        try
        {
            // connect to database
            $handle = new PDO("mysql:dbname=" . DATABASE . ";host=" . SERVER, USERNAME, PASSWORD);

            // ensure that PDO::prepare returns false when passed invalid SQL
            $handle->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 
        }
        catch (Exception $e)
        {
            // trigger (big, orange) error
            trigger_error($e->getMessage(), E_USER_ERROR);
            exit;
        }
    }

    // prepare SQL statement
    $statement = $handle->prepare($sql);
    if ($statement === false)
    {
        // trigger (big, orange) error
        trigger_error($handle->errorInfo()[2], E_USER_ERROR);
        exit;
    }

    // execute SQL statement
    $results = $statement->execute($parameters);

    // return result set's rows, if any
    if ($results !== false)
    {
        return $statement->fetchAll(PDO::FETCH_ASSOC);
    }
    else
    {
        return false;
    }
}

这是道歉功能

   /**
 * Apologizes to user with message.
 */
function apologize($message)
{
    render("apology.php", ["message" => $message]);
    exit;
}

和渲染功能。

    /**
 * Renders template, passing in values.
 */
function render($template, $values = [])
{
    // if template exists, render it
    if (file_exists("../templates/$template"))
    {
        // extract variables into local scope
        extract($values);

        // render header
        require("../templates/header.php");

        // render template
        require("../templates/$template");

        // render footer
        require("../templates/footer.php");
    }

    // else err
    else
    {
        trigger_error("Invalid template: $template", E_USER_ERROR);
    }
}
4

2 回答 2

1

尝试运行:

$confirmstatus = query("SELECT * FROM users WHERE username = ?", $_POST["username"]);
print_r($confirmstatus);

仔细检查您的函数是否实际上返回了一个数组。

也试试这个作为你的查询:

    $Checkusername = $_POST['username'];
   $confirmstatus = query("SELECT * FROM users WHERE username = '$Checkusername'");

还。您的

function query(/* $sql [, ... ] */)
{ }

传递给函数的变量已被注释掉。如果这与您的代码完全相同..那么我建议对以下内容进行修订:

function query ($sql)
{ .... }

编辑

 if ($confirmstatus["0"]['confirmed'] == 0)
    {
        apologize("Please first confirm your account.");
    }

我忽略了一些东西。似乎您正在返回一个多维数组您需要查看第一个数组(在本例中为 0),然后查找所需的字段。

于 2012-12-07T04:07:44.327 回答
0

改用这个: $confirmstatus 返回一个包含其余部分的数组

$confirmstatus = query("SELECT * FROM users WHERE username = ?", $_POST["username"]); if ($confirmstatus[0]["confirmed"] == 0) { sorry("请先确认您的账号。"); }

于 2014-08-27T12:12:08.970 回答