SQLSTATE[HY093]:无效的参数编号[string:异常:private]


SQLSTATE[HY093]: Invalid parameter number [string:Exception:private]

PDOException对象([message:protect]=>SQLSTATE[HY093]:无效的参数号[string:Exception:private]=>[code:protect]=>HY093[file:protect]]=>/home/unfd/public_html/src/php/auth/login.php[line:protect]]=>74[trace:Exception:private]=>Array=>PDOStatement[type]=>->[args]=>数组([0]=>数组([:用户名]=>测试))[1]=>数组([file]=>/home/unfd/public_html/src/php/auth/login.php[line]=>137[function]=>query[args]=>数组([0]=>INSERT INTO用户(用户名、密码、电子邮件、图像)VALUES(:用户名、:密码、:电子邮件、'test')[1]=>阵列([:用户名称]=>测试]=>测试)[3]=>阵列([:email]=>此处有一封电子邮件)))[2]=>数组([file]=>/home/unfd/public_html/signup/index.php[line]=>53[function]=>register[args]=>数组

不知道为什么会发生这种事。这是我的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代码上的注释标记,它们还没有更新为相关的。我不知道为什么会发生这种事。

您在这里的数组中缺少了一点,导致当您的查询应该准备好时,参数不匹配:

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'));

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