绑定变量的数量与PDO的令牌数量不匹配
问题描述:
我正在运行显示在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();
他们有一个值(请参阅我的问题上的var_dump),会话已经开始在我的'include(...)' – androidnation