简体   繁体   中英

mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables using array

I want to add some fields to a table but I get this error:

Fatal error: Call to a member function bind_param() on a non-object in C:\wamp\www\GuildSite\Include\Database.php on line 69

This is the function I have to insert into a table: I did move the $stmt === false before the foreach loop

public function myinsert($sqlInsert,$param)
{
$stmt = $this->_connection->prepare($sqlInsert) ;
if ($stmt === FALSE) 
{
  die($this->_connection->error);
}
echo 'Parameters number: ' . sizeof($param) ;
foreach ( $param as $key => $field )
{
  $stmt->bind_param ( $key , $field ) ;
}

$result = $stmt->execute() ;
$stmt->close();
return $result ;
}

From the page with the form, I have these parameters and this is how I call the function:

$param[':name'] =$_POST["personaName"];
$param[':email'] = $_POST["email"];
$param[':pass'] = $_POST['password'] ;
$sqlInsert = "INSERT INTO `new_member`(`persona_name`, `email`, `password`) "
      . "VALUES (':name', ':email', ':pass');";
$db->myinsert($sqlInsert , $param) ;

There was a small error in the $sqlInsert no ' around the values. This is sorted

What am I doing wrong?

I did copy the sql insert from the phpMyadmin after inserting a new row in the table.

I did try to run the execute

$result = $stmt->execute($param) ;

but it still wouldn't insert any fields in the table.

this is my database connection

public function __construct()
{
  try
  {
    $this->_connection = new mysqli(DB_SERVER,DB_USER,DB_PASS,DB_NAME) ;
  } 
  catch (Exception $e) 
  { 
    echo $e->errorMessage(); 
  }
}

When I did check the size of the array I got a size of 3

It seems you are using the wrong arguments to bind_param() . You must specify a string to indicate parameter types as a first argument. So you might retrieve the data type initials, as required by bind_param() , at runtime, like that:

$types = '';
foreach ( $param as $key => $field )
{
  $types .= gettype($field)[0];
}

Now that can be used as a first argument to your bind_param() . Also, looking at the documentation alone, the function doesn't seem to allow named parameters, only indexed ones. So, it seems like very difficult to continue using bind_param() to execute your query. The function doesn't lend itself well to your use case. You might use the PDO mysql interface and PDO::bindValue() instead which supports named parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM