简体   繁体   中英

Invalid parameter number PDO

got this:

public function Register($uname,$pass,$mail)
    {
        try
        {
            $new_password = password_hash($pass, PASSWORD_DEFAULT);
            $stmt = $this->db->prepare("INSERT INTO users(username,email,password)
            VALUES(:uname, :mail, :pass)");

            $stmt->bindparam(":uname, $uname");
            $stmt->bindparam(":mail, $mail");
            $stmt->bindparam(":pass, $new_password");
            $stmt->execute();
        }
        catch (PDOException $e)
        {
            echo $e->getMessage();
        }
    }

when I am trying to register it throws me an error:SQLSTATE[HY093]: Invalid parameter number: no parameters were bound. Was looking through the internet for the answers, but didn't find it yet, maybe you, guys , got some ideas?

You should use bindparam(':name', $name) instead of bindparam(':name, $name') . The first argument is a name of parameter slug, and the second argument is a variable you want to bind.

public function Register($uname,$pass,$mail)
{
    try
    {
        $new_password = password_hash($pass, PASSWORD_DEFAULT);
        $stmt = $this->db->prepare("INSERT INTO users(username,email,password)
        VALUES(:uname, :mail, :pass)");

        //note the quotes!
        $stmt->bindParam(":uname", $uname);
        $stmt->bindParam(":mail", $mail);
        $stmt->bindParam(":pass", $new_password);
        $stmt->execute();
    }
    catch (PDOException $e)
    {
        echo $e->getMessage();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM