简体   繁体   中英

Error: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

I need to send the name , lastname , number and email to a database. I created this code:

<?php
  echo $name;echo "<br>";
  echo $lastname;echo "<br>";
  echo $email;echo "<br>";
  echo $celnumber;echo "<br>";
  $servername = "localhost";
  $username = "usertest1";
  $password = "1234";
  $dbname="usertest1";
  try {
      $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
      // set the PDO error mode to exception
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

      // prepare sql and bind parameters
      $stmt = $conn->prepare("INSERT INTO tabela_script (name, lastname, email, celnumber)
      VALUES (:name, :lastname, :email, :celnumber)");
      $stmt->bindParam(':firstname', $name);
      $stmt->bindParam(':lastname', $lastname);
      $stmt->bindParam(':email', $email);
      $stmt->bindParam(':celnumber', $celnumber);
      $stmt->execute();

      echo "New record created successfully";
      }
  catch(PDOException $e)
      {
      echo "Error: " . $e->getMessage();
      echo "<br>";
      echo "<a href=\"main_html.html\"><h1>TRY AGAIN</h1></a>";
      }
  $conn = null;?>

And getting the error below:

Error: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

I have to try below code on my local server.

Change

$stmt->bindParam(':firstname', $name);

To

$stmt->bindParam(':name', $name);

The issue was exact name was not matched that's why to return the error like this Error: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

This error can happen when you have the wrong column name from your database in your PDO statement. Finally found that solution after trying many varieties of answers for this error.

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