1

I have this query:

qry=INSERT INTO assessments (Speaking_and_listening, Reading, Writing, Number, Measure, Geometry, Statistics, Science, ICT, Health_and_well_being, Relationships, Living_in_the_wider_world ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

I ceated two variables and a loop to do the binding:

foreach ($subjectListArray as $key=>$subject){
$sss .='s'; 
if (!isset($_POST[str_replace(" ","_",$subject)])){
    $fieldNameValues.='fred';   
}else{
    $fieldNameValues.=$_POST[str_replace(" ","_",$subject)];
}
if ($key!=(count($subjectListArray)-1)){
    $sss.=' ,';
    $fieldNameValues.=', ';
}
}

When I print out the two arrays $sss and $fieldNameValues I get this:

s ,s ,s ,s ,s ,s ,s ,s ,s ,s ,s ,s
p2iic, fred, fred, fred, fred, fred, fred, fred, fred, fred, fred, fred

Which looks right to me.

However when prepare and bind thus:

$record= $conn->prepare($qry);  
$record-> bind_param($sss, $fieldNameValues);

I get errors telling me

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

which as far as I can see it does - there are 12 in the $ary, and twelve in $sss and 12 in $fieldNamesValues

What's wrong please?

maxelcat
  • 1,333
  • 2
  • 18
  • 31
  • 1
    remove the commas. it should be just `$sss = "ssssssssssss";`. oh and i also think that you can't use that string for multiple parameters. should be `bind_param($sss,$param1, $param2,$param3)` etc. – Tanuel Mategi Nov 09 '15 at 12:18
  • d'oh about the commas!!! – maxelcat Nov 09 '15 at 14:46
  • Got it working Tanuel - so thanks, But it involves hard coding, and there must be a better way of doing it. – maxelcat Nov 09 '15 at 15:11
  • you are looking for [call_user_func_array()](http://php.net/manual/en/function.call-user-func-array.php). also take a look at http://stackoverflow.com/questions/16236395/bind-param-with-array-of-parameters – Tanuel Mategi Nov 09 '15 at 16:17
  • If you want to build the parameters from a dynamic array, you can implement this type of approach: https://stackoverflow.com/a/51036322/2943403 [str_replace](https://www.php.net/manual/en/function.str-replace.php) will accept an array as the 3rd parameter, so it can be called just once before the loop. – mickmackusa Apr 03 '22 at 08:49

0 Answers0