1

I was running through the errors displayed in this post since 2 days.

Here is the response from 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)

And here is the error I got:

exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens' in C:\wamp\www\dentist\pages\add_appoint.php:35 Stack trace:

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

Here is the PHP code:

<?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;
}
?>

Line 35 is $appExistStmt->execute();

Community
  • 1
  • 1
androidnation
  • 556
  • 1
  • 4
  • 17

1 Answers1

1
: time_app

Try remove space

:time_app
Arthur Khusnullin
  • 2,701
  • 12
  • 25