PHP Dynamic Prepared语句-元素数不匹配

PHP Dynamic Prepared语句-元素数不匹配,php,mysql,dynamic,prepared-statement,Php,Mysql,Dynamic,Prepared Statement,我正在尝试动态构造一个准备好的语句,但不断出现以下错误: mysqli_stmt_bind_param(): Number of elements in type definition string doesn't match number of bind variables in 当我回显我的语句时,类型定义的数量与bind变量匹配,所以我不知道是什么错了。我认为我的代码可能是在传递字符串、引号或其他东西,而不是变量,但我对准备好的语句不熟悉,不知道如何检查我的查询。当使用simplemy

我正在尝试动态构造一个准备好的语句,但不断出现以下错误:

mysqli_stmt_bind_param(): Number of elements in type definition string 
doesn't match number of bind variables in
当我回显我的语句时,类型定义的数量与bind变量匹配,所以我不知道是什么错了。我认为我的代码可能是在传递字符串、引号或其他东西,而不是变量,但我对准备好的语句不熟悉,不知道如何检查我的查询。当使用simple
mysqli\u查询时,我可以回显查询并查看我的错误在哪里。我不知道如何用准备好的陈述来做到这一点,所以我希望有人能帮助我发现我的错误

我正在尝试动态构造prepares语句,以便按如下方式重用代码:

$db = mysqli_stmt_init($dbconnection);

//i have looped through my fields and constructed a string that when echoed returns this: ?, ?, ?, ?, I use sub str just to remove the last comma and space leaving me with the string ?, ?, ?, ?. Ive echoed this to the browser to make sure it is correct.

$preparedQs = substr($preparedQs, 0, -2);

 //I then loop through each field using their datatype and constructs the type string as follows ssss. Ive echoed this to the browser to make sure it is correct.

$preparedType = 'ssss';

 //I then loop through my post array verifying and cleaning the data and then it constructing a string of clean values that results in Mike, null, Smith, Sr., (First, Middle, Last, Suffix) I use substr again just to remove the last comma and space. Ive echoed this to the browser to make sure it is correct.

    $cleanstr = substr($cleanstr, 0, -2);

 //I then explode that string into a an array that I can loop through and assign/bind each value to a variable as follows and use substr again to remove last comma and space.

    $cleanstr = explode(", ", $cleanstr);
    $ct2 = 0;
    foreach ( $cleanstr as $cl){
        $name = "a".$ct2;
        $$name = $cl;
        $varstr .= "$".$name.", ";
        $ct2 = $ct2 +1;    
    }
   $varstr = substr($varstr, 0, -2);

 //ive echoed the $varstr to the browser and get $a1, $a2, $a3, $a4. I have also echo their value outside of the loop and know values have been assigned.

 //I then try to assign each step above the appropriate prepared statement place holder

   $stmt = mysqli_stmt_prepare($db, "INSERT INTO Contacts VALUES (". $preparedQs. ")");
    mysqli_stmt_bind_param($db, "'".$preparedType."'", $varstr);
    mysqli_stmt_execute($stmt);
我不确定我做错了什么,因为当我回显
$preparedQs
$preparedType
$varstr
时,它们的元素数都相同,但我得到了“mysqli_stmt_bind_param():类型定义字符串中的元素数与..中的绑定变量数不匹配”错误。我所能想到的是,我有引号或一些我不应该做的事情,但我已经尝试在某些区域添加和删除引号,无法解决错误

此外,我读了一些关于在prepared语句中传递null的帖子,但即使用实际值替换null,我仍然会得到相同的错误

可能值得注意的是,当使用简单的过程性
mysqli\u查询
mysqli\u real\u escape\u字符串
来清理数据时,一切都很好。我试图通过将我的应用程序转换为准备好的语句来提高安全性,只是为了增加安全性

//编辑/不同

这个问题有两个不同的原因

  • 我使用的是过程编码,而不是对象或PDO。因此,对于准备好的陈述来说,这些例子是新的,即使在试图理解它们之后,也没有什么帮助
  • 我使用的是insert语句,而不是select或update语句,在过程php中,insert的查询字符串编写方式与select或update语句不同
    有人能帮我解决我的问题吗。谢谢。

    您正在以字符串形式传递
    $varstr
    ,而不是它所期望的实际单个变量。那么这应该是一个数组吗?如何将此字符串转换为单个变量?谢谢。请查看上面的编辑。@aynber&Shadow请查看编辑并重新打开我的问题。谢谢你。这和什么样的声明没有关系。执行行的构建是相同的。