3

ユーザーの IP、失敗した試行回数、および最後の試行時間を という名前の MySQL データベース テーブルに記録することにより、単純なログイン システム保護メカニズムを使用していますbannedusers。ただし、次のコードを使用してデータベースに新しいエントリを挿入しようとすると、execute() 関数は false を返し、実行に失敗します。

次のようにコードします。

private $con;

function updateTable($IP, $attempt, $exists){
    $time = time();

    //The following statement is actually in the constructor, moved here for completeness
    $this->con = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME); //All these constants are predefined and verified to be correct.

    if($this->con->connect_error){
        return true; //If there is a connection error, just let the user log in... We'll deal with this later
    }

    //Another function already determines if the entry exists or not.
    if(!$exists){
        //ip, retrycount, attempttime are the name of the fields. IP is a 40-char wide VARHCAR, retrycount is a tinyint and attempttime is a big int.
        $query = "INSERT INTO bannedusers (ip, retrycount, attempttime) VALUES (?,?,?)";

        if($stmt = $this->con->prepare($query)){

            //This following statement executes without throwing errors and returns true.
            $stmt->bind_param('sii', $IP, $attempt, $time);

            $successful = $stmt->execute();
            $stmt->close();

            if(!$successful){
                //Causes a small dialog to appear telling you the query failed.
                echo "<script type='text/javascript'>alert('Failed query!');</script>";
            }
        }
    }else{
        //Unrelated code omitted.
    }
}

私はphpとMySQLにかなり慣れていませんが、調査の結果、SQL構文では、クエリのVALUEセクションのフィールドを次のように引用符で囲む必要があることがわかりました。

$query = "INSERT INTO bannedusers (ip, retrycount, attempttime) VALUES ('?','?','?')";

しかし、ここで実際にクエリが機能しなくなることがわかりました(まだ試してみて、bind_param()でエラーが発生しました)。タイプを「sii」または「sss」または「ssi」に変更しようとしましたが、すべてクエリが失敗しました。SQL クエリの最後にセミコロンを追加しようとしましたが、何も変わりませんでした。すべての場合において、「失敗したクエリ!」ダイアログ ボックスがポップアップし、他のエラーは表示されません (上記の例外では、VALUES フィールドを引用符で囲みます。

どんな助けでも大歓迎です。

アップデート:

MySQLエラーレベルがMYSQLI_REPORT_ALLに上げられた場合にのみエラーを引き起こす関数に渡される前に、$ip何らかの形であることが判明しました。null

4

1 に答える 1

5

mysqli を例外モードに設定するか

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

または、常にすべての mysqli 操作の結果を確認し、mysqli エラーを手動でスローします。

$result = $stmt->execute();
if (!$result) {
    throw new Exception($mysqli->error);
}

これは、execute() の何が問題なのかを知る唯一の方法です。

SQL 構文では、VALUE のフィールドを引用符で囲む必要があるようです。

もちろん、それは間違っています。SQL 構文では、文字列のみを引用符で囲む必要があるようです。

于 2013-08-14T19:07:12.833 に答える