SQLSTATE [HY093]:参数号无效:循环时插入数据时未定义参数

时间:2018-05-18 06:30:45

标签: php loops pdo while-loop

我在循环语句中插入数据时遇到问题。我只是想在循环时创建一个插入数据查询,但它总是给我这个错误:“SQLSTATE [HY093]:参数号无效:参数未定义”。我不知道这个错误究竟是什么。 .................................................. .................................................. ................................................ 这是代码:

for ($i = 1; $i <= $select_stds_evaluation->rowCount(); $i++) {

$select_stds_evaluation_data = $select_stds_evaluation->fetch(PDO::FETCH_ASSOC);

/* Show the students data that was randomly picked */

$user_code = $select_stds_evaluation_data['user_code'];

$showStds = $this->db->prepare("SELECT * FROM students WHERE school_id = :user_code");
$showStds->bindparam(":user_code", $user_code);
$showStds->execute();

while ($stdsData = $showStds->fetch(PDO::FETCH_ASSOC)) {


    /* ## */
    /* [Start] Saving all the generated students evaluation to the database */
    /* ## */

    try {

        $evaluation_type = "student";
        $user_code = $stdsData['school_id'];
        $f_id = $faculty_data['f_id'];

        $save_genStdsData = $this->db->prepare("INSERT INTO generated_results (evaluation_type, user_code, f_id, semester, semestral_term, school_year) VALUES (:evaluation_type, :user_code, :f_id, :semester, :semestral_term, :school_year)");
        $save_genStdsData->bindparam(":evaluation_type", $evaluation_type);
        $save_genStdsData->bindparam(":$user_code", $user_code);
        $save_genStdsData->bindparam(':f_id', $f_id);
        $save_genStdsData->bindparam(":semester", $today_sem);
        $save_genStdsData->bindparam(":semestral_term", $today_sem_term);
        $save_genStdsData->bindparam(":school_year", $today_sy);
        $save_genStdsData->execute();

    } catch (PDOException $ex) {
        echo $ex->getMessage();
    }


    /* ## */
    /* [End] Saving all the generated students evaluation to the database */
    /* ## */



    ?>
    <br><?php echo $stdsData['firstname'] . ' ' . $stdsData['lastname']; ?><br>
    <?php

}

$std_categories = $this->db->prepare("SELECT * FROM evaluation_results WHERE f_id = :f_id AND evaluation_type = 'student' AND user_code = :user_code AND semester = :semester AND semestral_term = :semestral_term AND school_year = :school_year GROUP BY category");
$std_categories->bindparam(':f_id', $faculty_data['f_id']);
$std_categories->bindparam(":user_code", $select_stds_evaluation_data['user_code']);
$std_categories->bindparam(":semester", $today_sem);
$std_categories->bindparam(":semestral_term", $today_sem_term);
$std_categories->bindparam(":school_year", $today_sy);
$std_categories->execute();

$std_total_rank = 0;

while ($std_categ = $std_categories->fetch(PDO::FETCH_ASSOC)) {

    /* Get the category percentage */
    $get_categ_data = $this->db->prepare("SELECT * FROM categories WHERE category = :category");
    $get_categ_data->bindparam(":category", $std_categ['category']);
    $get_categ_data->execute();

    $categ_data = $get_categ_data->fetch(PDO::FETCH_ASSOC);

    $user_code = $select_stds_evaluation_data['user_code'];

    /* Get all the questions per category */
    $std_questions = $this->db->prepare("SELECT * FROM evaluation_results WHERE f_id = :f_id AND evaluation_type = 'student' AND user_code = :user_code AND semester = :semester AND semestral_term = :semestral_term AND school_year = :school_year");
    $std_questions->bindparam(':f_id', $faculty_data['f_id']);
    $std_questions->bindparam(":user_code", $user_code);
    $std_questions->bindparam(":semester", $today_sem);
    $std_questions->bindparam(":semestral_term", $today_sem_term);
    $std_questions->bindparam(":school_year", $today_sy);
    $std_questions->execute();

    $std_rate_per_rank = 0;

    while ($std_quest = $std_questions->fetch(PDO::FETCH_ASSOC)) {

        $std_rate_per_rank += $std_quest['rating'];

    }

    $std_rate_per_rank = $std_rate_per_rank * $categ_data['percentage'];

    $std_total_rank = $std_rate_per_rank;

}

}

1 个答案:

答案 0 :(得分:2)

我认为您的代码存在拼写错误

$save_genStdsData->bindparam(":$user_code", $user_code);

应该是

$save_genStdsData->bindparam(":user_code", $user_code);