【问题标题】:Number of bound variables does not match number of tokens with PDO绑定变量的数量与 PDO 的令牌数量不匹配
【发布时间】:2016-03-07 07:30:56
【问题描述】:

我遇到了this post since 2 days 中显示的错误。

这是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)

这是我得到的错误:

带有消息“SQLSTATE[HY093]”的异常“PDOException”:无效 参数号:绑定变量的数量与数量不匹配 C:\wamp\www\dentist\pages\add_apppoint.php:35 中的令牌' 堆栈跟踪:

0 C:\wamp\www\dentist\pages\add_apppoint.php(35): PDOStatement->execute() #1 {main}

这是 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-&gt;execute();

【问题讨论】:

  • 它们有一个值(请参阅我的问题顶部的 var_dump),并且会话已经在我的include(...)中启动

标签: php mysql


【解决方案1】:
: time_app

尝试删除空格

:time_app

【讨论】:

  • 哦,就是这样。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-31
  • 2014-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多