2016-03-07 133 views
1

我正在運行顯示在this post since 2 days中的錯誤。綁定變量的數量與PDO的令牌數量不匹配

這裏是var_dump($_POST)響應:

array (size=5) 
    'select_old' => string '2' (length=1) 
    'patient_name' => string '' (length=0) 
    'app_date' => string '2016-03-07' (length=10) 
    'app_time' => string '11:11' (length=5) 
    'app_reason' => string 'a' (length=1) 

這裏是我得到的錯誤:

異常 'PDOException' 有消息「SQLSTATE [HY093]:無效 參數號:號綁定變量的值不匹配 令牌的數量在C:\ wamp \ www \ dentist \ pages \ add_appoint.php中:35堆棧跟蹤:

0 C:\ w安培\ WWW \牙醫\頁面\ add_appoint.php(35):PDOStatement->執行()#1 {主}

這裏是PHP代碼:

<?php 
//Set error reporting on 
error_reporting(E_ALL); 
ini_set("display_errors", 1); 

//Include connection file 
require_once('../include/global.php'); 

//Json and PHP header 
header('Content-Type: application/json'); 

$user = $_SESSION['username']; 
$id_logged = $_SESSION['login_id']; 

try 
{ 
    $arr = array(); 
    //Values From AJAX 
    $patient_name = $_POST['patient_name']; 
    $date_app = $_POST['app_date']; 
    $time_app = $_POST['app_time']; 
    $reason = $_POST['app_reason']; 
    $old_patient_id = $_POST['select_old']; 
    //var_dump($_POST);exit(); 
    //If new patient 
    if($patient_name == "" && $old_patient_id != 0) 
    { 

     //See if date and time exist 
     $appExist = "SELECT * FROM appointment WHERE id_logged = :id_logged AND date_app = :date_app and time_app = : time_app"; 
     $appExistStmt = $conn->prepare($appExist); 
     $appExistStmt->bindValue(":id_logged", $id_logged); 
     $appExistStmt->bindValue(":date_app", $date_app); 
     $appExistStmt->bindValue(":time_app", $time_app); 
     $appExistStmt->execute(); 
     $appExistStmtCount = $appExistStmt->rowCount(); 

     if($appExistStmtCount === 0) 
     { 
      //Add to appointment table 
      $appAdd = "INSERT INTO appointment(id_logged, patient_id, date_app, time_app, reason) 
         VALUES(:id_logged, :patient_id, :date_app, :time_app, :reason)"; 
      $appAddStmt = $conn->prepare($appAdd); 
      $appAddStmt->bindValue(':id_logged', $id_logged); 
      $appAddStmt->bindValue(':patient_id', $old_patient_id, PDO::PARAM_INT); 
      $appAddStmt->bindValue(':date_app', $date_app); 
      $appAddStmt->bindValue(':time_app', $time_app); 
      $appAddStmt->bindValue(':reason', $reason); 

      $appAddStmt->execute(); 

      echo "added"; 
     } 
     else 
     { 
      echo "Not Added"; 
     } 
    } 

    //If patient name exist 
    if($patient_name != "" && $old_patient_id == 0) 
    { 
     //add new patient 
     $addNewPatient = "INSERT INTO patient(patient_name, id_logged) VALUES(:patient_name, :id_logged)"; 
     $addNewPatientStmt = $conn->prepare($addNewPatient); 
     $addNewPatientStmt->bindValue(":patient_name", $patient_name); 
     $addNewPatientStmt->bindValue(":id_logged", $id_logged); 
     $addNewPatientStmt->execute(); 

     $lastId = $conn->lastInsertId(); 

     //See if date and time exist 
     $appExist = "SELECT * FROM appointment WHERE id_logged = :id_logged AND date_app = :date_app and time_app = : time_app"; 
     $appExistStmt = $conn->prepare($appExist); 
     $appExistStmt->bindValue(":id_logged", $id_logged); 
     $appExistStmt->bindValue(":date_app", $date_app); 
     $appExistStmt->bindValue(":time_app", $time_app); 
     $appExistStmt->execute(); 
     $appExistStmtCount = $appExistStmt->rowCount(); 

     if($appExistStmtCount == 0) 
     { 
      //Add to appointment table 
      $appAdd = "INSERT INTO appointment(id_logged, patient_id, date_app, time_app, reason) 
         VALUES(:id_logged, :patient_id, :date_app, :time_app, :reason)"; 
      $appAddStmt = $conn->prepare($appAdd); 
      $appAddStmt->bindValue(":id_logged", $id_logged); 
      $appAddStmt->bindValue(":patient_id", $lastId); 
      $appAddStmt->bindValue(":date_app", $date_app); 
      $appAddStmt->bindValue(":time_app", $time_app); 
      $appAddStmt->bindValue(":reason", $reason); 

      $appAddStmt->execute(); 


      $arr = array('patient_name'=>$patient_name, 'date_app' =>$date_app); 
      echo json_encode($arr); 
     } 
     else 
     { 
      $msg = "Their is another existing appointment in the same time, please specify another date and time"; 
      $arr = array('patient_name'=>$msg, 'date_app', $date_app, 'time_app', $time_app); 
     } 
    } 
} 
catch(PDOException $m) 
{ 
    $m->getMessage(); 
    echo "error".$m; 
} 
?> 

第35行是$appExistStmt->execute();

+0

他們有一個值(請參閱我的問題上的var_dump),會話已經開始在我的'include(...)' – androidnation

回答

1
: time_app 

嘗試刪除空間

:time_app 
+0

哦,就是這樣。謝謝。 – androidnation

相關問題