110 lines
4.2 KiB
PHP
Executable File
110 lines
4.2 KiB
PHP
Executable File
<?php
|
|
include "../../connect.php";
|
|
|
|
|
|
|
|
try {
|
|
// Collect and validate input data
|
|
$requiredFields = ["phone", "email", "password", "gender", "birthdate", "first_name", "last_name"];
|
|
$inputData = [];
|
|
|
|
foreach ($requiredFields as $field) {
|
|
$inputData[$field] = filterRequest($field);
|
|
if (empty($inputData[$field])) {
|
|
printFailure("Missing required field: $field");
|
|
}
|
|
}
|
|
|
|
// Collect optional fields
|
|
$optionalFields = [
|
|
"id", "license_type", "national_number", "name_arabic", "name_english",
|
|
"issue_date", "expiry_date", "license_categories", "address", "card_id",
|
|
"occupation", "licenseIssueDate", "religion", "status", "site",
|
|
"education", "employmentType", "maritalStatus"
|
|
];
|
|
|
|
foreach ($optionalFields as $field) {
|
|
$inputData[$field] = filterRequest($field);
|
|
}
|
|
|
|
// Generate driver ID if not provided
|
|
// $inputData['id'] = empty($inputData['id']) ? sha1(uniqid()) : $inputData['id'];
|
|
|
|
// Hash password
|
|
$inputData['hashedPassword'] = password_hash($inputData['password'], PASSWORD_DEFAULT);
|
|
|
|
// Set default site if not specified
|
|
$inputData['site'] = ($inputData['site'] === null || $inputData['site'] === "") ? "Not specified" : $inputData['site'];
|
|
|
|
// Check if the phone number or email address already exists
|
|
$stmt = $con->prepare("SELECT * FROM driver WHERE phone = :phone OR email = :email");
|
|
$stmt->bindParam(':phone', $inputData['phone'], PDO::PARAM_STR);
|
|
$stmt->bindParam(':email', $inputData['email'], PDO::PARAM_STR);
|
|
$stmt->execute();
|
|
|
|
if ($stmt->rowCount() > 0) {
|
|
printFailure("The email or phone number is already registered.");
|
|
}
|
|
|
|
// Prepare SQL for inserting new driver
|
|
$sql = "
|
|
INSERT INTO `driver` (
|
|
`id`, `phone`, `email`, `password`, `gender`, `license_type`, `national_number`,
|
|
`name_arabic`, `name_english`, `issue_date`, `expiry_date`, `license_categories`,
|
|
`address`, `card_id`, `occupation`, `licenseIssueDate`, `religion`, `status`,
|
|
`birthdate`, `site`, `first_name`, `last_name`, `education`,
|
|
`employmentType`, `maritalStatus`, `created_at`, `updated_at`
|
|
) VALUES (
|
|
:id, :phone, :email, :hashedPassword, :gender, :license_type, :national_number,
|
|
:name_arabic, :name_english, :issue_date, :expiry_date, :license_categories,
|
|
:address, :card_id, :occupation, :licenseIssueDate, :religion, :status,
|
|
:birthdate, :site, :first_name, :last_name, :education,
|
|
:employmentType, :maritalStatus, NOW(), NOW()
|
|
)
|
|
";
|
|
|
|
$stmt = $con->prepare($sql);
|
|
if (!$stmt) {
|
|
throw new Exception("Failed to prepare statement: " . $con->error);
|
|
}
|
|
|
|
// Bind parameters
|
|
$paramsToBind = [
|
|
'id', 'phone', 'email', 'hashedPassword', 'gender', 'license_type', 'national_number',
|
|
'name_arabic', 'name_english', 'issue_date', 'expiry_date', 'license_categories',
|
|
'address', 'card_id', 'occupation', 'licenseIssueDate', 'religion', 'status',
|
|
'birthdate', 'site', 'first_name', 'last_name', 'education',
|
|
'employmentType', 'maritalStatus'
|
|
];
|
|
|
|
foreach ($paramsToBind as $param) {
|
|
if (isset($inputData[$param])) {
|
|
$stmt->bindValue(":$param", $inputData[$param], PDO::PARAM_STR);
|
|
} else {
|
|
$stmt->bindValue(":$param", null, PDO::PARAM_STR);
|
|
}
|
|
}
|
|
|
|
// Log SQL and parameters for debugging
|
|
error_log("SQL: " . $sql);
|
|
error_log("Params: " . print_r($inputData, true));
|
|
|
|
// Execute the statement
|
|
if (!$stmt->execute()) {
|
|
throw new Exception("Failed to execute statement: " . $stmt->errorInfo()[2]);
|
|
}
|
|
|
|
// Check if the record was successfully saved
|
|
if ($stmt->rowCount() > 0) {
|
|
printSuccess($inputData['id']);
|
|
} else {
|
|
printFailure("Failed to save driver data");
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
error_log("Database error: " . $e->getMessage());
|
|
printFailure("A database error occurred: " . $e->getMessage());
|
|
} catch (Exception $e) {
|
|
error_log("An error occurred: " . $e->getMessage());
|
|
printFailure("An error occurred: " . $e->getMessage());
|
|
} |