Fatal Error : Call to a Member Function bind_param()

Asked

Viewed 3,655 times

0

I’m getting this problem:

Fatal error: Call to a Member Function bind_param() on Boolean in C: xampp htdocs Myworld Register create_user.php on line 32

When executing this code:

  $email = htmlspecialchars($_POST["email"]);
  $pw1 = $_POST["pw1"];
  $pw2 = $_POST["pw2"];
  $nome = $_POST["nome"];
  $sobrenome = $_POST["sobrenome"];
  $date = $_POST["dataN"];

  if($pw1 === $pw2){

    $hash = password_hash($pw1, PASSWORD_DEFAULT);
    $lig = new mysqli("localhost", "root", "", "myworld");
    if($lig->connect_error){
      die("<p>Serviço indisponível. Por favor tente mais tarde</p>");
    }
    $sql = "INSERT INTO utilizadores(email, password, nome, sobrenome, data_nascimento) VALUES(?, ?, ?, ?, ?,)";
    $inst = $lig->prepare($sql);
    $inst->bind_param("ssiss", $email, $hash, $nome, $sobrenome, $date);
    if($inst->execute() === TRUE){
      echo "<p>Bem vindo, " . $email . "</p>";
    }
    else{
      echo "<p>Registo não efetuado. Email indisponível</p>";
    }
    $lig->close();

  }
  else{
    echo "<p>As passwords não coincidem! Por favor <a href='index.html'>repita o registo</a></p>";
  }

1 answer

0


This error is because you tried to access the bind_param of the object of mysqli instead of the object generated by prepare, always follow the documentation examples:

The documentation exists for this and contains examples for most cases, so your code should be:

  $email = htmlspecialchars($_POST["email"]);
  $pw1 = $_POST["pw1"];
  $pw2 = $_POST["pw2"];
  $nome = $_POST["nome"];
  $sobrenome = $_POST["sobrenome"];
  $date = $_POST["dataN"];

  if($pw1 === $pw2){

    $hash = password_hash($pw1, PASSWORD_DEFAULT);
    $lig = new mysqli("localhost", "root", "", "myworld");
    if($lig->connect_error){
      die("<p>Serviço indisponível. Por favor tente mais tarde</p>");
    }
    $sql = "INSERT INTO utilizadores(email, password, nome, sobrenome, data_nascimento) VALUES(?, ?, ?, ?, ?,)";

    $stmt = $lig->prepare($sql);
    $stmt->bind_param("ssiss", $email, $hash, $nome, $sobrenome, $date);

    if($stmt->execute() === TRUE){
      echo "<p>Bem vindo, " . $email . "</p>";
    }
    else{
      echo "<p>Registo não efetuado. Email indisponível</p>";
    }
    $stmt->close();

  }
  else{
    echo "<p>As passwords não coincidem! Por favor <a href='index.html'>repita o registo</a></p>";
  }

  $lig->close();
  • I’ve solved the problem, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.