sql >> データベース >  >> RDS >> Mysql

mysqli_query()は少なくとも2つのパラメータを必要とします&mysqli_query():空のクエリエラーメッセージ

    欠落しているmysqli接続リソース/オブジェクトに加えて、スクリプトには他のいくつかの問題があります:

    例えば

    <?php
    define('MYSQL_ER_DUP_KEY', 1022); // see https://dev.mysql.com/doc/refman/5.6/en/error-messages-server.html#error_er_dup_key
    $errors = array();
    if($_POST) // might be superfluous
    {
        // simplified email validation
        // improve if needed
        $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
        if ( !$email ) {
            // removed html/style from error message, better do that when printing the error
            $errors['email1'] = "A valid email address is required";
        }
    
        // you only need the database connection after the email address is validated
        $mysqli = new mysqli('localhost', 'root', '','ecommerce');
        // see http://docs.php.net/mysqli.quickstart.connections
        if ($mysqli->connect_errno) {
            trigger_error("Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error, E_USER_ERROR);
        }
    
        // not checking if this email address is already in the database
        // instead create a unique index for that field
        // see https://dev.mysql.com/doc/refman/5.6/en/constraint-primary-key.html
        // - otherwise you'd at least have to lock the table to avoid race conditions -
    
        // sql injections: see http://docs.php.net/security.database.sql-injection
        // to prevent sql injections you either have to make sure string literals are
        // properly encoded/escaped or use preparead statements+parameters
        $stmt = $mysqli->prepare('INSERT INTO subscriptions (email) VALUES (?)');
        if ( !$stmt ) {
            trigger_error("prepare statement failed (" . $mysqli->errno . ") " . $mysqli->error, E_USER_ERROR);
        }
        else if ( !$stmt->bind_param('s', $email) ) {
            trigger_error("bind_param failed (" . $stmt->errno . ") " . $stmt->error, E_USER_ERROR);
        }
        else if ( !$stmt->execute() ) {
            // email has a unique index, inserting an email address a second time
            // results in a ER_DUP_KEY error
            if ( MYSQL_ER_DUP_KEY==$stmt->errno ) {
                $errors['email2'] = "email address already in subsription list";
            }
            else { // otherwise it's "really" an error
                trigger_error("execute failed (" . $stmt->errno . ") " . $stmt->error, E_USER_ERROR);
            }
        }
        else {
          [... inserted ...]
        }
    }
    


    1. OracleSQLクエリで繰り返される行を削除する方法

    2. 1年以内の月ごとのカウントを取得するPostgresqlクエリ

    3. ドット表記の代わりにPostgreSQLクエリで機能表記を使用する

    4. 複雑なSQLコマンドを実装する方法