1

I am getting an error back on my INSERT query

Fatal error: Call to a member function bind_param() on boolean in...

although I am only inserting strings into the table columns which are expecting string values :: VARCHAR(255)

My table looks like this:

+---------------+--------------+-----+------+---+--+--+
| id            | int(11)      | NO  | PRI  |   |  |  |
| username      | varchar(45)  | YES | UNI  |   |  |  |
| email_address | varchar(45)  | YES | UNI  |   |  |  |
| password      | varchar(255) | YES |      |   |  |  |
| role          | int(11)      | YES | NULL | 0 |  |  |
| dashboard_id  | int(11)      | YES | NULL |   |  |  |
+---------------+--------------+-----+------+---+--+--+

And my Php looks like this...

    $stmt = $conn->prepare("INSERT INTO users VALUES (?, ?, ?)");
    $stmt->bind_param('sss', $username, $emailAddress, $upassword);

    $username = $_POST['username'];
    $emailAddress = $_POST['email'];
    $upassword = password_hash($_POST['upassword'], PASSWORD_DEFAULT);

    $stmt->execute();

    printf("%d Row inserted.\n", $stmt->affected_rows);

    $stmt->close();
    $conn->close();

UPDATE

My connection code:

$conn = new mysqli($db_host,$db_user,$db_password,$db_name);
3
  • Fatal error: Call to a member function bind_param() on boolean in... would indicate that $stmt is false, and therefore your prepare hasn't worked. Commented Feb 15, 2017 at 11:42
  • I know this already... Any suggestions...? Commented Feb 15, 2017 at 11:48
  • Have you checked your connection details? Tried them using MySQL Workbench or similar other application? Commented Feb 15, 2017 at 12:14

1 Answer 1

2

This is working at my local end:-

Structure of table:-

Name            Type            Null    Default  Extra
id              int(11)         No      None     AUTO_INCREMENT  Primary Key
user_name       varchar(256)    No      None    
email_address   varchar(256)    No      None
password        varchar(256)    No      None
role            int(11)         Yes     0
dashboard_id    int(11)         Yes     0

Php code:-

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

$conn = new mysqli('*****','***','****','****');
$conn->set_charset("utf8");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$_POST['username'] = 'anant'; // i have hard coded for checking
$_POST['email'] = '[email protected]';// i have hard coded for checking
$_POST['upassword'] = 'anant@123';// i have hard coded for checking
if(!empty(trim($_POST['username'])) && !empty(trim($_POST['email'])) && !empty(trim($_POST['upassword']))){
 $username = $_POST['username'];
 $emailAddress = $_POST['email'];
 $upassword = password_hash($_POST['upassword'], PASSWORD_DEFAULT);

  if($stmt = $conn->prepare("INSERT INTO users (user_name,email_address,password) VALUES (?, ?, ?)")){

      $stmt->bind_param('sss', $username, $emailAddress, $upassword);

      $stmt->execute();
      $stmt->close();
    }else{
        echo $conn->error;
    }
  $conn->close();
}else{
  echo "form values are not coming";
}
?>

Note:-

I got the problem, it is about not providing column names inside prepare(). When i did echo $conn->error; then i got this error Column count doesn't match value count at row 1.

So when i provided corresponding column name everything fine and started working.

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.