1

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
deni1myftiu
  • 29
  • 1
  • 1
  • 2

3 Answers3

4

Placeholders in the SQL query do not match parameters bound in bindParam(). Check them one by one.

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

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Kamlesh Solanki
  • 616
  • 5
  • 10
0

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.

Spinstaz
  • 287
  • 6
  • 12
0

What I experienced was that when you have a '.' or '-' in placeholders and you do bind the right number of parameters, you can get this error. I figured out that changing the field names (and thus the placeholders) solved the problem