Getting message error when inserting by bindParam()

I have register form execute PHP code like this :

$stmt = $connection->prepare("INSERT INTO users  VALUES (?, ?, ?, ?, ?, ?, ?)");

       $stmt->bindParam("isssiii", $id, $username, $password,$email,$s_id,$m_id,$d_id);
       $id="";
       $username=$myusername;
       $password=$mypassword;
       $email=$myemail;
       $s_id="1";
       $m_id="1";
       $d_id="1";

i get error : Fatal error: Uncaught Error: Call to a member function bindParam() on bool
How can i solve this?

This most likely means the value of $stmt is false, as a result of a problem with your query causing the prepare to fail.
When you make an INSERT query, you must tell the database which coulmns you want to populate with the data.
Eg.

"INSERT INTO users (col1, col2, col3, col4, col5, col6, col7)  VALUES (?, ?, ?, ?, ?, ?, ?)"

As an aside, you don’t have to use bindParam with PDO, you can pass in an array with execute.

@SamA74
thanks alot
now i get another error : Fatal error: Uncaught Error: Call to undefined method mysqli_stmt::bindParam()

It should be bind_Param not bindParam.
Though I see now it’s mysqli you are using, not PDO. It’s such along time since I used mysqli, I don’t really rememer the syntax too well. I switched to PDO and never looked back, it’s way better.

1 Like

@SamA74
Opps How i didn’t figure that?
thanks alot

Looking at the exampple here, maybe you don’t have to state which columns (if it’s all of them).
https://www.php.net/manual/en/mysqli-stmt.bind-param.php
I was just looking for a reason why the query failed.

Shouldn’t these come before the bindParam?

@Gandalf
no it is worked now, just i typed the bind_param wrong

1 Like

I always thought it was odd/wrong that you can bind parameters to variables, before the variables were defined, but apparently you can.

1 Like

Explicit parameter binding uses a reference to the variables, that is evaluated at the time of the execute() call. An unfortunate side effect of this is that there won’t be any php undefined variable/index errors if there’s a mistake in a variable/index name (yet another reason to use the PDO extension instead.)

As to finding out why the prepare() call failed. You ALWAYS need error handling for statements that can fail. For database statements, the easiest way of adding error handling for all the statements that can fail (connection, query, prepare, and execute), without adding logic at each statement, is to use exceptions for errors and in most cases let php catch the exception, where it will use its error related settings to control what happens with the actual error information (database statement errors will ‘automatically’ get displayed/logged the same as php errors.) The exception to this rule is when inserting/updating user submitted data and there’s a duplicate or out of range value. In this case, your code should catch the exception, detect if the sql error number is for something your code is designed to handle, then setup an error message for the visitor telling them what was wrong with the data that they submitted. For all other sql error numbers, just re-throw the exception and let php handle it. To enable exceptions for errors for the mysqli extension, add the following line of code before the point where you make the database connection, then remove any existing mysqli statement error handling logic since it will no longer be executed if there is an error -

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.