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

时间:2016-07-22 07:35:35

标签: php mysql pdo

我这里有问题。我一直得到这个错误,这是绑定变量的数量与令牌的数量不匹配。我仔细检查我的代码,似乎没有错误。无论如何,我是这些PDO的新手。

这是我如何将数据从数据库调用到以

形式回显的代码
<form action="editpage.php" method="post" class="login, reminder">
<p>Student Matric Number:
    <input type="text" class="login-input" name="addmatricno" id="addmatricno" value="<?php echo $r['matricno'] ?>" placeholder="Enter student matric number">
    Student Name:
    <input type="text" class="login-input" name="addname" id="addname" value="<?php echo $r['studentname'] ?>" placeholder="Enter student name">
    Programme:
    <input type="text" class="login-input" name="addprogramme" id="addprogramme" value="<?php echo $r['programme'] ?>" placeholder="Enter student programme">
    Education Level:

    <?php

        if($r['education_level'] == "PHD"){
            echo '<select name="selectedulevel" id="selectedulevel" class="login-input">';
            echo '<option disabled>Please select education level</option>';
            echo '<option value="PHD" selected>PHD</option>';
            echo '<option value="MASTER">MASTER</option>';
            echo '<option value="PHD">DEGREE</option></p></select>';
        }
        else if($r['education_level'] == "MASTER"){
            echo '<select name="selectedulevel" id="selectedulevel" class="login-input">';
            echo '<option disabled>Please select education level</option>';
            echo '<option value="PHD">PHD</option>';
            echo '<option value="MASTER" selected>MASTER</option>';
            echo '<option value="PHD">DEGREE</option></p></select>';
        }
        else{
            echo '<select name="selectedulevel" id="selectedulevel" class="login-input">';
            echo '<option disabled>Please select education level</option>';
            echo '<option value="PHD">PHD</option>';
            echo '<option value="MASTER">MASTER</option>';
            echo '<option value="PHD" selected>DEGREE</option></p></select>';
        }

    ?>
    <input type="submit" name="updatestudent" id="updatestudent" value="Update Student" class="login-submit" style = "width: 20%; text-align: center">

这是我的HTML代码

$query = " 
        UPDATE student
            SET
                matricno = :addmatricno,
                studentname = :addname,
                programme = :addprogramme,
                education_level = :selectedulevel
            WHERE
                id = :sessionidstudent
    "; 

    $query_params = array( 
        ':addmatricno' => $_POST['addmatricno'], 
        ':addname' => $_POST['addname'], 
        ':addprogramme' => $_POST['addprogramme'], 
        ':selectedulevel' => $_POST['selectedulevel']
    ); 

    try 
    { 
        // Execute the query to create the user 
        $stmt = $db->prepare($query); 
        //$stmt->bindValue(':sessionidstudent',$sessionidstudent);
        $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex) 
    { 
        // Note: On a production website, you should not output $ex->getMessage(). 
        // It may provide an attacker with helpful information about your code.  
        die("Failed to run query: " . $ex->getMessage()); 
    }


    // This redirects the user back to the login page after they register 
    echo '<script language="javascript">';
    echo 'alert("Info updated successful.")';
    echo '</script>'; 
    header("Refresh: 0; updatebooks.php"); 
    die(); 

最后但并非最不重要的是,我的查询代码。

$db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);

注意:$ db引用我放在其他文件的Typing test,我可以确定我与数据库的连接完全没有问题。 我已多次检查我的代码但仍然得到相同的错误。

1 个答案:

答案 0 :(得分:1)

在查询params变量中,您忘记在更新查询中添加:sessionidstudent,添加该错误并且错误将消失