mysqli_stmt::bind_param():类型定义字符串中的元素数与绑定变量数不匹配

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

提问人:Samuel Stiles 提问时间:6/21/2013 最后编辑:DharmanSamuel Stiles 更新时间:10/21/2022 访问量:59763

问:

因此,我有一个非常令人头疼的查询,我需要执行涉及 65 个表单输入,需要使用 mysqli 准备好的语句注入数据库。

我遇到的问题是它说我尝试调用的变量的 # 与我正在使用的“s”的 # 不匹配。我数了十几次,看不出我在这里哪里出了问题。有 65 个变量和 65 个“s”。bind_param

谁能看到我遗漏的东西?还是我可能以不正确的方式使用了bind_param方法?

// Preparing our query statement via mysqli which will auto-escape all bad characters to prevent injection
$query3 = 'INSERT INTO datashep_AMS.COMPLETE_APPLICATIONS (
    project_name,
    status,
    funding_requested,
    project_title,
    program,
    county,
    parish,
    name_of_watercourse,
    which_is_a_tributary_of,
    name_of_applicant,
    contact_person_or_project_supervisor,
    relationship_to_organization,
    business_phone,
    home_phone,
    email,
    signature_of_thesis_or_study_supervisor,
    mailing_address,
    postal_code,
    website,
    mailing_address_for_payment,
    hst_registration_no,
    total_cost_dollar,
    total_cost_percent,
    dollar_amount_requested_from_nbwtf,
    percent_amount_requested_from_nbwtf,
    descriptive_summary,
    background_of_organization,
    mandate,
    years_in_existence,
    membership,
    accomplishments,
    previous_project_name,
    previous_project_number,
    previous_project_amount_received_from_nbwtf,
    summary_of_activities,
    summary_of_Results,
    project_title_2,
    reason_and_or_purpose,
    objectives,
    project_description,
    methods,
    equipment_and_materials_required,
    personnel_required,
    proposed_start_date,
    proposed_end_date,
    type_of_data_to_be_stored,
    where_will_it_be_housed,
    monitoring,
    short_term_achievement,
    long_term_achievement,
    previous_studies,
    required_permits,
    consultants,
    short_term_commitment,
    long_term_commitment,
    project_duration,
    project_evaluation,
    promotion_of_project,
    promotion_of_client,
    publication_of_results,
    community_benefits,
    effects_on_traditional_uses,
    possible_changes_in_public_access_to_areas,
    possible_impact_on_wildlife_and_or_environment,
    likelihood_of_future_requests_for_funding,
    list_all_other_funding_sources_for_this_project
) VALUES (
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?,
    ?
)';

// "Preparing" the query using mysqli->prepare(query) -- which is the equivalent of mysql_real_escape_string -- in other words, it's the SAFE database injection method
$stmt = $dbConnection->prepare($query3);

// "Bind_param" == replace all the "?"'s in the aforementioned query with the variables below

$stmt->bind_param("s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s", $project_name, $status, $funding_requested, $project_title, $program, $county, $parish, $name_of_watercourse, $which_is_a_tributary_of, $name_of_applicant, $contact_person_or_project_supervisor, $relationship_to_organization, $business_phone, $home_phone, $email, $signature_of_thesis_or_study_supervisor, $mailing_address, $postal_code, $website, $mailing_address_for_payment, $hst_registration_no, $total_cost_dollar, $total_cost_percent, $dollar_amount_requested_from_nbwtf, $percent_amount_requested_from_nbwtf, $descriptive_summary, $background_of_organization, $mandate, $years_in_existence, $membership, $accomplishments, $previous_project_name, $previous_project_number, $previous_project_amount_received_from_nbwtf, $summary_of_activities, $summary_of_Results, $project_title_2, $reason_and_or_purpose, $objectives, $project_description, $methods, $equipment_and_materials_required, $personnel_required, $proposed_start_date, $proposed_end_date, $type_of_data_to_be_stored, $where_will_it_be_housed, $monitoring, $short_term_commitment, $long_term_achievement, $previous_studies, $required_permits, $consultants, $short_term_commitment, $long_term_commitment, $project_duration, $project_evaluation, $promotion_of_project, $promotion_of_client, $publication_of_results, $community_benefits, $effects_on_traditional_uses, $possible_changes_in_public_access_to_areas, $possible_impact_on_wildlife_and_or_environment, $likelihood_of_future_requests_for_funding, $list_all_other_funding_sources_for_this_project);

// Perform the actual query!
$stmt->execute();
php mysql mysqli 预备语句

评论

1赞 Your Common Sense 6/21/2013
你从哪里得到逗号分隔的格式?
0赞 Samuel Stiles 6/21/2013
完全披露:我是初学者,所以我真的不知道更好的方法(还)。
0赞 lonesomeday 6/21/2013
@SamuelStiles我认为你真的应该考虑数据库表中是否需要 65 列。如果您有多个表和联接,它可能会使其更易于管理。
0赞 SuKu 11/27/2019
@Your 常识:PHP插入查询的当前和更好的方法是什么?
0赞 Your Common Sense 11/27/2019
@SuKu对于常规插入查询,只需使用预处理语句即可

答:

43赞 lonesomeday 6/21/2013 #1

类型定义字符串中的字符数必须等于 SQL 查询中的占位符(?标记)数。

                                                    // three ? marks
$stmt->prepare("INSERT INTO table (one, two, three) VALUES (?,?,?)");
$stmt->bind_param("sss", $one, $two, $three);
               // three s characters (and three variables as well)

请注意,类型定义字符串中的字符不应用逗号分隔。

您可以在手册页上的示例中看到此格式的演示。

15赞 PokeSteelProductions 5/2/2016 #2

有 65 个字符串参数,所以如果有 65 个 s,你就有正确的数字。但是,出现错误是因为您用逗号分隔了 s。而不是它应该是$stmt->bind_param("s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s..."

$stmt->bind_param("sssssssssssssssss..."

这应该可以解决您的错误。

3赞 Gaurav Singh 8/23/2019 #3

中的字符不应用逗号分隔。bind_param

如果必须绑定两个变量,请执行以下操作:

$stmt->bind_param('ss', $firstvariable, $secondvariable);