markasoftware
markasoftware

Reputation: 12672

bind_param number arguments not equal to variables

I am having an error in mysql and php My code:

$stmt->prepare("INSERT `tablename` (col1,col2,col3) VALUES (?,?,?)");
$stmt->bind_param('sss',$_POST['data'],$sub,$yaxis);
$stmt->execute();

My errors:

Warning: mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of variables doesn't match number of parameters in prepared statement. 

But I have passed 3s and 3 arguments in bind_param. Why is this error occuring?

Upvotes: 1

Views: 2095

Answers (4)

markasoftware
markasoftware

Reputation: 12672

Okay, I know this makes me sound stupid but the error was caused by an undefined variable farther up in the script, which I have now fixed. I thought it would not affect this part of the script, but it did. All the answers posted are great, but because of my fault they do not answer the question.

Upvotes: 0

Victor
Victor

Reputation: 3

Well, either $_POST has more than one variable assigned to it, or it has none.

This will print the contents of the entire $_POST array:

print_r($_POST);

Upvotes: 0

Sibu
Sibu

Reputation: 4617

Are you sure you have value in your $_POST['data'] , Instead of directly passing form values to bind_param you can check if it is null or not.Also i noticed your insert query is wrong

if(!empty($_POST['data'])){
$mydata=$_POST['data'];
}else{
$mydata='No values';
}

$stmt->prepare("INSERT into `tablename` (col1,col2,col3) VALUES (?,?,?)");//Query wrong
$stmt->bind_param('sss',$mydata,$sub,$yaxis);
$stmt->execute();

Upvotes: 0

John Woo
John Woo

Reputation: 263883

use the index of parameter since you did not specify it by name.

$data = $_POST['data'];
$stmt->prepare("INSERT `tablename` (col1,col2,col3) VALUES (?,?,?)");
$stmt->bind_param(1,$data);
$stmt->bind_param(2,$sub);
$stmt->bind_param(3,$yaxis);
$stmt->execute();

but if you want it by name then you have to specify its parameter name instead of using question mark

$data = $_POST['data'];
$stmt->prepare("INSERT `tablename` (col1,col2,col3) VALUES (:sss,:par1,:par2)");
$stmt->bind_param(":sss",$data);
$stmt->bind_param(":par1",$sub);
$stmt->bind_param(":par2",$yaxis);
$stmt->execute();

Upvotes: 2

Related Questions