【问题标题】:SQLSTATE[HY093]: Invalid parameter number [string:Exception:private]SQLSTATE[HY093]:参数号无效 [string:Exception:private]
【发布时间】:2016-03-04 09:46:27
【问题描述】:

PDOException Object ( [message:protected] => SQLSTATE[HY093]: Invalid parameter number [string:Exception:private] => [code:protected] => HY093 [file:protected] => /home/unfed/ public_html/src/php/auth/login.php [line:protected] => 74 [trace:Exception:private] => Array ([0] => Array ([file] => /home/unfed/public_html/src /php/auth/login.php [line] => 74 [function] => execute [class] => PDOStatement [type] => -> [args] => Array ( [0] => Array ( [:username ] => test ) ) ) [1] => Array ([file] => /home/unfed/public_html/src/php/auth/login.php [line] => 137 [function] => query [args] => Array ([0] => INSERT INTO users (username,password,email,image) VALUES (:username,:password,:email,'test') [1] => Array ([:username] => test ) [2] => 数组 ( [:password] => test ) [3] => 数组 ( [:email] => 这里有一封电子邮件 ) ) ) [2] => 数组 ( [file] => /home /unfed/public_html/signup/index.php [line] => 53 [function] => register [args] => Array ( [0] => test [1] => Theres an email here [2] => test) ) ) [previous:Exception:private] => [errorInfo] => Array ([0] => HY093))

不知道为什么会这样。 这是我的 PHP:

//Create a new function named LoggedIn, And apply database info;
function Connect($host = 'localhost',$username = 'unfed_admin',$password = 'x',$dbname = 'unfed_auth') {

    //Try execute the PHP with no errors;
    try {

        //Create a PDO Session;
        $con = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);

        //Session Attributes;
        $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

    }

    //Catch all PDOException errors;
    catch (PDOException $e) {

        //If any errors print result;
        echo "<code><pre>".print_r($e)."</pre></code>";

        //Make the PDO session false;
        $con = false;
    }

    //If no errors happened Make the PDO session true;
    return $con;
}
//Create a new function named query;
function query($sql = false,$bind = false,$obj = false) {

    //Prepare The SQL Query;
    $query = Connect()->prepare($sql);

    $res = true;

    //Execute Binded Query;
    try { $query->execute($bind); $res = true; }

    catch (PDOException $e) { 

        //If any errors print result;
        echo "<code><pre>".print_r($e)."</pre></code>";

        $res = false;

    }

    //If no errors happened Make $row true;
    return $res;

}
//Create a new function named user_login;
function register($username = false, $email = false, $password = false) {

    global $registeroutput;

    //Fetch for the username and email to see if they are already used;
    $fetchusername = fetch("SELECT username FROM users WHERE username = :username",array(":username"=>$username));
    $fetchemail = fetch("SELECT email FROM users WHERE email = :email",array(":email"=>$email));
    if(empty($fetchusername)) {

        if(empty($fetchemail)) {

            //Register the user;
            query("INSERT INTO users (username,password,email,image) VALUES (:username,:password,:email,'test')",array(":username"=>$username),array(":password"=>$password),array(":email"=>$email));
            $registeroutput = 'Success';

        } else { $registeroutput = 'Email Error'; }

    } else { $registeroutput = 'Username Error'; }

    //If no errors happened Make the $valid true;
    return true;

}

忽略 PHP 代码上的注释标记,它们没有更新为相关。我不知道为什么会这样。

【问题讨论】:

  • 你真的应该使用 PHP 的 built-in functions 来处理密码安全问题。如果您使用的 PHP 版本低于 5.5,则可以使用 password_hash() compatibility pack。
  • @JayBlanchard 我知道我使用它。只是明白这不相关。我想知道为什么它不会将电子邮件密码和用户名插入数据库。

标签: php mysql


【解决方案1】:

您在此处的数组中遗漏了一点,导致当您的查询假定准备好时,参数不匹配:

query("INSERT INTO users (username,password,email,image) VALUES (:username,:password,:email,'test')",array(":username"=>$username),array(":password"=>$password),array(":email"=>$email));

为了解决这个问题,您可以进行以下更改:

query("INSERT INTO users (username,password,email,image) VALUES (:username,:password,:email,:image)",array(":username"=>$username),array(":password"=>$password),array(":email"=>$email), array(":image"=>'test'));

注意:image在VALUES子句中使用,然后添加到数组中。

【讨论】:

  • 这不是问题,但它让我走上了正确的轨道,意味着只有一个 array() 只是将所有 :image 等放在一个数组中。谢谢!
猜你喜欢
  • 2023-01-09
  • 2019-09-28
  • 2013-08-04
  • 2016-12-04
  • 2016-01-13
  • 1970-01-01
相关资源
最近更新 更多