2013-06-21 84 views
5

Vì vậy, tôi có truy vấn gây đau đầu lớn mà tôi cần thực hiện liên quan đến các đầu vào biểu mẫu cần nhập vào cơ sở dữ liệu bằng cách sử dụng các câu lệnh được chuẩn bị mysqli.mysqli_stmt :: bind_param(): Số phần tử trong chuỗi định nghĩa loại không khớp với số biến liên kết

Vấn đề tôi đang gặp phải là nó cho biết số biến tôi đang cố gắng gọi bind_param trên không khớp với số "s" mà tôi đang sử dụng. Tôi đếm một chục lần và không thấy nơi tôi đang đi sai ở đây. Có 65 biến và 65 "s".

Có ai có thể nhìn thấy thứ tôi đang thiếu không? Hoặc tôi có lẽ sử dụng phương pháp bind_param một cách không chính xác?

// 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(); 
+0

bạn đã lấy định dạng được phân tách bằng dấu phẩy đó ở đâu? –

+0

Và tôi vẫn không hiểu tại sao người dùng PHP lại * có xu hướng viết hàng trăm kilobyte mã cho truy vấn chèn đơn giản. –

+0

Tiết lộ đầy đủ: Tôi là người mới bắt đầu nên tôi thực sự không biết cách nào tốt hơn (chưa). –

Trả lời

16

Các nhân vật trong chuỗi không nên được phân cách bằng dấu phẩy:

$stmt->bind_param("sss...", /* variables */); 

Bạn có thể thấy định dạng này thể hiện trong các ví dụ trên the manual page.

+0

Điều này dường như là vấn đề của tôi! Cảm ơn! –

3

có 65 tham số chuỗi vì vậy nếu có 65 s bạn có số chính xác. Tuy nhiên, các lỗi xuất hiện vì bạn đã tách các dấu phẩy bằng dấu phẩy. Thay vì $stmt->bind_param("s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s..." , nó phải là

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

Điều này sẽ giải quyết được lỗi của bạn.

+0

Điều này giải quyết được vấn đề của tôi. – AndroidHacker

+0

Điều này đã giúp tôi rất nhiều thời gian !! Cảm ơn bạn, PokeSteelSản xuất. –