I looked around stackoverflow, there are lots of this kind of question. But I can’t seem to find what is missing in the code.
Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:xampphtdocsbackendcasestraffictraffic-insert.php:95 Stack trace: #0 C:xampphtdocsbackendcasestraffictraffic-insert.php(95): PDOStatement->execute(Array) #1 C:xampphtdocsrouter.php(51): include_once('C:\xampp\htdocs...') #2 C:xampphtdocsrouter.php(9): route('/traffic-insert', '/backend/cases/...') #3 C:xampphtdocsroutes.php(24): post('/traffic-insert', '/backend/cases/...') #4 {main} thrown in C:xampphtdocsbackendcasestraffictraffic-insert.php on line 95
This error came after solving this:
PHP AJAX – data update/edit inserts as new data instead of updating
Here is the code:
<?php
include('./backend/config/connection.php');
include('./backend/config/function.php');
if( isset($_POST["traffic_operation"]) ) {
if( isset($_POST["traffic_operation"]) == "Add" ) {
$traffic_doc = '';
if( $_FILES["traffic_doc"]["name"] != '') {
$traffic_doc = upload_image();
}
$statement = $connection->prepare('INSERT INTO traffic_violations (
plateNumber,
carModel,
carColor,
violationType,
ownerGender,
violationDateTime,
violationLocation,
workingShift,
violationAction,
violationStatement,
cccEmployee
) VALUES (
:plate_number,
:car_model,
:car_color,
:violation_type,
:owner_gender,
:violation_date,
:violation_location,
:working_shift,
:violation_action,
:traffic_doc,
:ccc_employee
)
');
$result = $statement->execute(
array(
':plate_number' => $_POST["plate_number"],
':car_model' => $_POST["car_model"],
':car_color' => $_POST["car_color"],
':violation_type' => $_POST["violation_type"],
':owner_gender' => $_POST['owner_gender'],
':violation_date' => $_POST['violation_date'],
':violation_location' => $_POST['violation_location'],
':working_shift' => $_POST['working_shift'],
':violation_action' => $_POST['violation_action'],
':traffic_doc' => $traffic_doc,
':ccc_employee' => $_POST['ccc_employee']
)
);
if( !empty($result) ) {
echo '<script>alert("Traffic Violation Added")</script>';
}
}
if( $_POST["traffic_operation"] == "Edit" ) {
$traffic_doc = '';
if( $_FILES["traffic_doc"]["name"] != '') {
$traffic_doc = upload_image();
} else {
$traffic_doc = $_POST['hidden_user_image'];
}
$statement = $connection->prepare('UPDATE traffic_violations SET
plateNumber = :plate_number,
carModel = car_model,
carColor = car_color,
violationType = violation_type,
ownerGender = owner_gender,
violationDateTime = violation_date,
violationLocation = violation_location,
workingShift = working_shift,
violationAction = violation_action,
violationStatement = traffic_doc,
cccEmployee = ccc_employee,
WHERE id = :id'
);
$statement->execute(
array(
'id' => $_POST["violation_id"],
':plate_number' => $_POST["plate_number"],
':car_model' => $_POST["car_model"],
':car_color' => $_POST["car_color"],
':violation_type' => $_POST["violation_type"],
':owner_gender' => $_POST['owner_gender'],
':violation_date' => $_POST['violation_date'],
':violation_location' => $_POST['violation_location'],
':working_shift' => $_POST['working_shift'],
':violation_action' => $_POST['violation_action'],
':traffic_doc' => $traffic_doc,
':ccc_employee' => $_POST['ccc_employee']
)
);
echo 'Traffic Violation Updated';
}
}
?>