2016-07-22 53 views
0

我在這裏遇到問題。我不斷收到這個綁定變量數量與令牌數量不匹配的錯誤。我仔細檢查我的代碼,似乎沒有錯誤。無論如何,我是這些PDO的新手。無法運行查詢:SQLSTATE [HY093]:無效的參數編號:綁定變量的數量與令牌數量不匹配

這是我的代碼怎麼稱呼從數據庫中的數據形式

if(isset($_GET['idstudent'], $_GET['idbook'])){ 
$_SESSION['link']=$_GET['idstudent']; 
$_SESSION['link2']=$_GET['idbook']; 
$sessionidstudent = $_SESSION['link']; 
$sessionidbook = $_SESSION['link2']; } 


$query = " 
    SELECT 
     * 
    FROM viewlibrary 
    WHERE 
     id = :sessionidstudent AND 
     serialno = :sessionidbook 

"; 

$query_params = array( 
    ':sessionidstudent' => $_SESSION['link'], 
    ':sessionidbook' => $_SESSION['link2'] 
); 

try 
{ 
    $stmt = $db->prepare($query); 
    $result = $stmt->execute($query_params); 
} 
catch(PDOException $ex) 
{ 
    die("Failed to run query: " . $ex->getMessage()); 
} 
$r=$stmt->fetch(PDO::FETCH_ASSOC); ?> 

呼應這是我的HTML代碼

<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"> 

最後但並非最不重要的,我的查詢代碼。

$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是指$db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);這裏我把其他的文件,我可以肯定,我的數據庫連接有一點問題都沒有。 我檢查了我的代碼很多次,但仍然得到相同的錯誤。

+0

忘記更新查詢bind'sessionidstudent'! – Saty

+0

是的,我忘了。抱歉。尷尬。雖然我不知道WHERE子句後面的變量也需要放在查詢參數中。 – AhKing

回答

1

在查詢參數變量,你忘了加上:sessionidstudent更新查詢,添加和錯誤將會消失

+0

.....哦,這很尷尬。我不應該發佈這個問題。不過多虧了你,至少我知道WHERE子句後面的變量也需要放在查詢參數中。我不知道。感謝和抱歉的問題:) – AhKing

+0

這很好,不客氣,如果它確實解決了您的問題,請點擊複選標記圖標將您的問題標記爲已回答!祝你今天愉快! – FMashiro

相關問題