PDO - 参数号无效

时间:2016-04-10 16:09:43

标签: php mysql pdo

下面是PDO更新查询,但我不断收到这些错误:

  

array(3){[0] => string(5)" 00000" [1] => NULL [2] => NULL}

     

SQLSTATE [HY093]:参数号无效:绑定变量数   与令牌数量不匹配

我最近转而使用PDO,所以不要吝啬。在搜索这些错误后,我发现查询问题很少,因此我们不知道为什么会出现这些错误。

  <?php
    error_reporting(E_ALL); ini_set("display_errors", 1);

    if(isset($_POST["update_data"])){
    // connect to the database

      $servername = 'HOST';
      $username = 'USER';
      $password = 'PASS';
      $dbname = 'TABLE';
      $current_user = $_SESSION['user_name'];

    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $sql = "UPDATE table SET 
                user_name = :user_name, 
                user_email = :user_email,  
                user_phone = :user_phone,  
                user_gender = :user_gender,
                employment_status = :employment_status,
                user_min_salary = :user_min_salary,
                user_dob = :user_dob,
                user_location = :user_location,
                user_years_experience = :user_years_experience,
                job_title = :job_title,
                company_name = :company_name,
                user_responsibilities = :user_responsibilities,
                job_duration = :job_duration,
                job_title_two = :job_title_two,
                company_name_two = :company_name_two,
                user_responsibilities_two = :user_responsibilities_two,
                job_duration_two = :job_duration_two,
                job_title_three = :job_title_three,
                company_name_three = :company_name_three,
                user_responsibilities_three = :user_responsibilities_three,
                job_duration_three = :job_duration_three,
                job_title_four = :job_title_four,
                company_name_four = :company_name_four,
                user_responsibilities_four = :user_responsibilities_four,
                job_duration_four = :job_duration_four,
                job_title_five = :job_title_five,
                company_name_five = :company_name_five,
                user_responsibilities_five = :user_responsibilities_five,
                job_duration_five = :job_duration_five,
                edu_orgainzation = :edu_orgainzation,
                edu_subject = :edu_subject,
                edu_completion = :edu_completion,
                edu_orgainzation_two = :edu_orgainzation_two,
                edu_subject_two = :edu_subject_two,
                edu_completion_two = :edu_completion_two,
                edu_orgainzation_three = :edu_orgainzation_three,
                edu_subject_three = :edu_subject_three,
                edu_completion_three = :edu_completion_three,
                edu_orgainzation_four = :edu_orgainzation_four,
                edu_subject_four = :edu_subject_four,
                edu_completion_four = :edu_completion_four,
                edu_orgainzation_five = :edu_orgainzation_five,
                edu_subject_five = :edu_subject_five,
                edu_completion_five = :edu_completion_five,
                language_short = :language_short,
                cover_letter = :cover_letter,
                last_update = NOW( )
                WHERE user_id = $current_user";

    $stmt = $conn->prepare($sql);

    var_dump($dbh->errorInfo());     

    $stmt->bindParam(':user_name', $_POST['user_name'], PDO::PARAM_STR);       
    $stmt->bindParam(':user_email', $_POST['$user_email'], PDO::PARAM_STR);    
    $stmt->bindParam(':user_phone', $_POST['user_phone'], PDO::PARAM_STR); 
    $stmt->bindParam(':user_gender', $_POST['user_gender'], PDO::PARAM_STR); 
    $stmt->bindParam(':employment_status', $_POST['employment_status'], PDO::PARAM_STR);   
    $stmt->bindParam(':user_min_salary', $_POST['user_min_salary'], PDO::PARAM_STR);   
    $stmt->bindParam(':user_dob', $_POST['user_dob'], PDO::PARAM_STR);   
    $stmt->bindParam(':user_location', $_POST['user_location'], PDO::PARAM_STR);   
    $stmt->bindParam(':user_years_experience', $_POST['user_years_experience'], PDO::PARAM_STR);   
    $stmt->bindParam(':job_title', $_POST['job_title'], PDO::PARAM_STR);   
    $stmt->bindParam(':company_name', $_POST['company_name'], PDO::PARAM_STR);   
    $stmt->bindParam(':user_responsibilities', $_POST['user_responsibilities'], PDO::PARAM_STR);   
    $stmt->bindParam(':job_duration', $_POST['job_duration'], PDO::PARAM_STR);   
    $stmt->bindParam(':job_title_two', $_POST['job_title_two'], PDO::PARAM_STR);   
    $stmt->bindParam(':company_name_two', $_POST['company_name_two'], PDO::PARAM_STR);   
    $stmt->bindParam(':user_responsibilities_two', $_POST['user_responsibilities_two'], PDO::PARAM_STR);   
    $stmt->bindParam(':job_duration_two', $_POST['job_duration_two'], PDO::PARAM_STR);   
    $stmt->bindParam(':job_title_three', $_POST['job_title_three'], PDO::PARAM_STR);   
    $stmt->bindParam(':company_name_three', $_POST['company_name_three'], PDO::PARAM_STR);   
    $stmt->bindParam(':user_responsibilities_three', $_POST['user_responsibilities_three'], PDO::PARAM_STR);   
    $stmt->bindParam(':job_duration_three', $_POST['job_duration_three'], PDO::PARAM_STR);   
    $stmt->bindParam(':job_title_four', $_POST['job_title_four'], PDO::PARAM_STR);   
    $stmt->bindParam(':company_name_four', $_POST['company_name_four'], PDO::PARAM_STR);   
    $stmt->bindParam(':user_responsibilities_four', $_POST['user_responsibilities_four'], PDO::PARAM_STR);   
    $stmt->bindParam(':job_duration_four', $_POST['job_duration_four'], PDO::PARAM_STR);   
    $stmt->bindParam(':job_title_five', $_POST['job_title_five'], PDO::PARAM_STR);   
    $stmt->bindParam(':company_name_five', $_POST['company_name_five'], PDO::PARAM_STR);   
    $stmt->bindParam(':user_responsibilities_five', $_POST['user_responsibilities_five'], PDO::PARAM_STR);   
    $stmt->bindParam(':job_duration_five', $_POST['job_duration_five'], PDO::PARAM_STR);   
    $stmt->bindParam(':edu_orgainzation', $_POST['edu_orgainzation'], PDO::PARAM_STR);   
    $stmt->bindParam(':edu_subject', $_POST['edu_subject'], PDO::PARAM_STR);   
    $stmt->bindParam(':edu_completion', $_POST['edu_completion'], PDO::PARAM_STR);
    $stmt->bindParam(':edu_orgainzation_two', $_POST['edu_orgainzation_two'], PDO::PARAM_STR);   
    $stmt->bindParam(':edu_subject_two', $_POST['edu_subject_two'], PDO::PARAM_STR);   
    $stmt->bindParam(':edu_completion_two', $_POST['edu_completion_two'], PDO::PARAM_STR);
    $stmt->bindParam(':edu_orgainzation_three', $_POST['edu_orgainzation_three'], PDO::PARAM_STR);   
    $stmt->bindParam(':edu_subject_three', $_POST['edu_subject_three'], PDO::PARAM_STR);   
    $stmt->bindParam(':edu_completion_three', $_POST['edu_completion_three'], PDO::PARAM_STR);
    $stmt->bindParam(':edu_orgainzation_four', $_POST['edu_orgainzation_four'], PDO::PARAM_STR);   
    $stmt->bindParam(':edu_subject_four', $_POST['edu_subject_four'], PDO::PARAM_STR);   
    $stmt->bindParam(':edu_completion_four', $_POST['edu_completion_four'], PDO::PARAM_STR);
    $stmt->bindParam(':edu_orgainzation_five', $_POST['edu_orgainzation_five'], PDO::PARAM_STR);   
    $stmt->bindParam(':edu_subject_five', $_POST['edu_subject_five'], PDO::PARAM_STR);   
    $stmt->bindParam(':edu_completion_five', $_POST['edu_completion_five'], PDO::PARAM_STR);
    $stmt->bindParam(':language_short', $_POST['language_short'], PDO::PARAM_STR);
    $stmt->bindParam(':cover_letter', $_POST['cover_letter'], PDO::PARAM_STR);
    $stmt->bindParam(':last_update', $_POST['last_update'], PDO::PARAM_STR);

    $stmt->execute(); 


        // echo a message to say the UPDATE succeeded
       echo 'success';
        }
    catch(PDOException $e)
        {
        echo $sql . "<br>" . $e->getMessage();
        }

    $conn = null;
    }
    ?>

非常感谢你!

1 个答案:

答案 0 :(得分:1)

删除此行:

$stmt->bindParam(':last_update', $_POST['last_update'], PDO::PARAM_STR);

您不需要在此处绑定参数,因为您正在使用MySQL函数NOW()。就像错误消息所说的那样,您的令牌数量与绑定参数的数量不匹配。