PHP AJAX - 未捕获的 PDOException:SQLSTATE[HY093],这是什么?
PHP AJAX - Uncaught PDOException: SQLSTATE[HY093], what is this?
我查了一下Whosebug,这类问题很多。但是我似乎找不到代码中缺少的内容。
Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\backend\cases\traffic\traffic-insert.php:95 Stack trace: #0 C:\xampp\htdocs\backend\cases\traffic\traffic-insert.php(95): PDOStatement->execute(Array) #1 C:\xampp\htdocs\router.php(51): include_once('C:\xampp\htdocs...') #2 C:\xampp\htdocs\router.php(9): route('/traffic-insert', '/backend/cases/...') #3 C:\xampp\htdocs\routes.php(24): post('/traffic-insert', '/backend/cases/...') #4 {main} thrown in C:\xampp\htdocs\backend\cases\traffic\traffic-insert.php on line 95
解决此问题后出现此错误:
PHP AJAX - data update/edit inserts as new data instead of updating
代码如下:
<?php
include('./backend/config/connection.php');
include('./backend/config/function.php');
if( isset($_POST["traffic_operation"]) ) {
if( $_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';
}
}
?>
问题在于逗号:
cccEmployee = :ccc_employee,
WHERE id = :id'
我查了一下Whosebug,这类问题很多。但是我似乎找不到代码中缺少的内容。
Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\backend\cases\traffic\traffic-insert.php:95 Stack trace: #0 C:\xampp\htdocs\backend\cases\traffic\traffic-insert.php(95): PDOStatement->execute(Array) #1 C:\xampp\htdocs\router.php(51): include_once('C:\xampp\htdocs...') #2 C:\xampp\htdocs\router.php(9): route('/traffic-insert', '/backend/cases/...') #3 C:\xampp\htdocs\routes.php(24): post('/traffic-insert', '/backend/cases/...') #4 {main} thrown in C:\xampp\htdocs\backend\cases\traffic\traffic-insert.php on line 95
解决此问题后出现此错误:
PHP AJAX - data update/edit inserts as new data instead of updating
代码如下:
<?php
include('./backend/config/connection.php');
include('./backend/config/function.php');
if( isset($_POST["traffic_operation"]) ) {
if( $_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';
}
}
?>
问题在于逗号:
cccEmployee = :ccc_employee,
WHERE id = :id'