I keep getting an error Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
at 'password' => $hashedpwd]);
(I have marked it in my code as well where the error is as in commented it out)
Full error: Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in /srv/disk7/3574321/www/userinfo.mywebcommunity.org/backend/signupbackend.php:78 Stack trace: #0 /srv/disk7/3574321/www/userinfo.mywebcommunity.org/backend/signupbackend.php(78): PDOStatement->execute(Array) #1 {main} thrown in /srv/disk7/3574321/www/userinfo.mywebcommunity.org/backend/signupbackend.php on line 78
I have marked that line as in commented out (line 78) Sorry, I have some other code which I have commented out in my code, so I did removed it before posting here as I am not using it,letting you know cause here I only have 75 lines so people don't get confused. But, I have marked the exact line that I have error in.
I went to through multiple questions on SO, but nothing worked for me. I even checked again, but I am only passing 3 values into INSERT and executing 3 values as well, so I am not sure what am I doing wrong. Can anyone reckon something? Also, my code is for signing up for a user on my website and store their details in database using SQL.
My code:
<?php
if(isset($_POST["signup_submit"])) {
require "../database_files/database_for_signup.php";
require "../index.php";
$username = $_POST['username'];
$email = $_POST['mail'];
$password = $_POST['password'];
$repeatPassword = $_POST['repeatpassword'];
if (empty($username) || empty($email) || empty($password) || empty($repeatPassword)) {
header("Location: ../index.php?error=emptyfields&username=" .$username."&mail=" .$email);
exit();
} else if(!filter_var($email, FILTER_VALIDATE_EMAIL) && !preg_match("/^[a-zA-Z0-9]*$/",$username)) {
header("Location: ../index.php?error=invalidmailusername");
exit();
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location: ../index.php?error=invalidmail&username=".$username);
exit();
} else if (!preg_match("/^[a-zA-Z0-9]*$/",$username)) {
header("Location: ../index.php?error=invalidusername&mail=".$email);
exit();
} else if($password != $repeatPassword) {
header("Location: ../index.php?error=passwordcheck&username=".$username."&mail=".$email);
exit();
} else {
try {
$sql = $conn->prepare("SELECT username FROM signup_info");
$sql->bindParam(":username", $_POST['username']);
$sql->execute();
} catch(PDOException $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
$resultCheck = $sql->setFetchMode(PDO::FETCH_ASSOC);
if($sql->rowCount() > 0) {
header("Location: ../index.php?error=usertaken&mail=".$email);
exit();
} else {
try {
$sql = $conn->prepare('INSERT INTO signup_info(username, email, `password`) VALUES(:username,:email,:`password`)');
} catch(PDOException $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
$hashedpwd = password_hash($password, PASSWORD_DEFAULT);
$sql->execute(['username' => $username,
'email' => $email,
'password' => $hashedpwd]); //error here
$sql->bind_param("sss", $username, $email, $hashedpwd);
$sql->execute();
header("Location: ../index.php?signup=success");
exit();
}
}
}
?>