Home > Back-end >  Fatal error: Uncaught PDOException: SQLSTATE[HY093]:
Fatal error: Uncaught PDOException: SQLSTATE[HY093]:

Time:04-20

If someone can help me with this, I'm not good with php but I'm trying to learn but this problem I encounter makes my head hurt.

Here's the full error:

Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\website\register.php:34 Stack trace: #0 C:\xampp\htdocs\website\register.php(34): PDOStatement->execute(Array) #1 {main} thrown in C:\xampp\htdocs\website\register.php on line 34

<?php

include 'config.php';

if(isset($_POST['submit'])){

   $name = $_POST['name'];
   $name = filter_var($name, FILTER_SANITIZE_STRING);
   $email = $_POST['email'];
   $email = filter_var($email, FILTER_SANITIZE_STRING);
   $pass = md5($_POST['pass']);
   $pass = filter_var($pass, FILTER_SANITIZE_STRING);
   $cpass = md5($_POST['cpass']);
   $cpass = filter_var($cpass, FILTER_SANITIZE_STRING);

   $user_type = $_POST['user_type'];
   $image = $_FILES['image']['name'];
   $image_tmp_name = $_FILES['image']['tmp_name'];
   $image_size = $_FILES['image']['size'];
   $image_folder = 'uploaded_img/'.$image;

   $select = $conn->prepare("SELECT * FROM `users` WHERE email = ?");
   $select->execute([$email]);

   if($select->rowCount() > 0){
      $message[] = 'user already exist!';
   }else{
      if($pass != $cpass){
         $message[] = 'confirm password not matched!';
      }elseif($image_size > 2000000){
         $message[] = 'image size is too large!';
      }else{
         $insert = $conn->prepare("INSERT INTO `users`(name, email, password, image, user_type) VALUES(?,?,?,?)");
         $insert->execute([$name, $email, $cpass, $image, $user_type]);
         if($insert){
            move_uploaded_file($image_tmp_name, $image_folder);
            $message[] = 'registered succesfully!';
            header('location:login.php');
         }
      }
   }

}

?>

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>register</title>

   <!-- font awesome cdn link  -->
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css">

   <!-- custom css file link  -->
   <link rel="stylesheet" href="css/style.css">

</head>
<body>



<?php
   if(isset($message)){
      foreach($message as $message){
         echo '
         <div >
            <span>'.$message.'</span>
            <i  onclick="this.parentElement.remove();"></i>
         </div>
         ';
      }
   }
?>
   
<section >

   <form action="" method="post" enctype="multipart/form-data">
      <h3>register now</h3>
      <input type="text" required placeholder="enter your username"  name="name">
      <input type="email" required placeholder="enter your email"  name="email">
      <input type="password" required placeholder="enter your password"  name="pass">
      <input type="password" required placeholder="confirm your password"  name="cpass">
      <input type="file" name="image" required  accept="image/jpg, image/png, image/jpeg">
      <select name="user_type">
 <option value="admin">admin</option>
         <option value="user">user</option>
         <option value="company">company</option>
      <p>already have an account? <a href="login.php">login now</a></p>
      <input type="submit" value="register now"  name="submit">
   </form>

</section>

</body>
</html>

CodePudding user response:

Change this line:

$insert = $conn->prepare("INSERT INTO `users`(name, email, password, image, user_type) 
      VALUES(?,?,?,?)");

to:

$insert = $conn->prepare("INSERT INTO `users`(name, email, password, image, user_type) 
      VALUES(?,?,?,?,?)");

Because you want to use 5 parameters , you also need 5 ? marks.

  •  Tags:  
  • php
  • Related