128 lines
5.1 KiB
PHP
128 lines
5.1 KiB
PHP
<?php
|
||
require_once __DIR__ . '/../../connect.php';
|
||
|
||
// 🚀 1. تسجيل بداية الطلب
|
||
error_log("🚀 [add_ride.php] Request Started.");
|
||
|
||
// استلام البيانات
|
||
$start_location = filterRequest("start_location");
|
||
$end_location = filterRequest("end_location");
|
||
$date_raw = filterRequest("date"); // نستلم القيمة الخام
|
||
$time_raw = filterRequest("time"); // نستلم القيمة الخام
|
||
$endtime_raw = filterRequest("endtime");
|
||
$price = filterRequest("price");
|
||
$passenger_id = filterRequest("passenger_id");
|
||
$driver_id = filterRequest("driver_id");
|
||
$status = filterRequest("status");
|
||
$price_for_driver = filterRequest("price_for_driver");
|
||
$price_for_passenger = filterRequest("price_for_passenger");
|
||
$distance = filterRequest("distance");
|
||
$carType = filterRequest("carType");
|
||
|
||
error_log("ℹ️ [add_ride.php] Data Received. Processing dates...");
|
||
|
||
// 🛠️ 2. معالجة التواريخ لتناسب MySQL (الحل الجذري للمشكلة)
|
||
// تحويل "2025-12-18 09:48:26.805" إلى "2025-12-18" فقط
|
||
$date_formatted = date("Y-m-d", strtotime($date_raw));
|
||
|
||
// تحويل "2025-12-18 09:48:26.810" إلى "09:48:26" فقط
|
||
$time_formatted = date("H:i:s", strtotime($time_raw));
|
||
|
||
// معالجة وقت الانتهاء (قد يكون مدة أو وقت)
|
||
// نحاول استخراج الوقت منه، إذا فشل نضعه 00:00:00
|
||
$endtime_formatted = date("H:i:s", strtotime($endtime_raw));
|
||
if (!$endtime_formatted) {
|
||
$endtime_formatted = "00:00:00";
|
||
}
|
||
|
||
// تجهيز مصفوفة البيانات
|
||
$data = [
|
||
":start_location" => $start_location,
|
||
":end_location" => $end_location,
|
||
":date" => $date_formatted, // نستخدم الصيغة المعالجة
|
||
":time" => $time_formatted, // نستخدم الصيغة المعالجة
|
||
":endtime" => $endtime_formatted,
|
||
":price" => $price,
|
||
":passenger_id" => $passenger_id,
|
||
":driver_id" => $driver_id,
|
||
":status" => $status,
|
||
":carType" => $carType,
|
||
":price_for_driver" => $price_for_driver,
|
||
":price_for_passenger" => $price_for_passenger,
|
||
":distance" => $distance,
|
||
];
|
||
|
||
// تسجيل البيانات التي سيتم إدخالها للتأكد
|
||
error_log("ℹ️ [add_ride.php] Prepared Data: " . json_encode($data));
|
||
|
||
// ---------------------------------------------------------
|
||
// 3. الإضافة في السيرفر المحلي (Main DB)
|
||
// ---------------------------------------------------------
|
||
|
||
$sql = "INSERT INTO `ride` (
|
||
`start_location`, `end_location`, `date`, `time`, `endtime`,
|
||
`price`, `passenger_id`, `driver_id`, `status`, `carType`,
|
||
`price_for_driver`, `price_for_passenger`, `distance`
|
||
) VALUES (
|
||
:start_location, :end_location, :date, :time, :endtime,
|
||
:price, :passenger_id, :driver_id, :status, :carType,
|
||
:price_for_driver, :price_for_passenger, :distance
|
||
)";
|
||
|
||
try {
|
||
error_log("🔄 [add_ride.php] Inserting into LOCAL DB...");
|
||
|
||
$stmt = $con->prepare($sql);
|
||
$stmt->execute($data);
|
||
|
||
$insertedId = $con->lastInsertId();
|
||
$count = $stmt->rowCount();
|
||
|
||
error_log("✅ [add_ride.php] Local Insert Success. ID: $insertedId");
|
||
|
||
if ($count > 0) {
|
||
|
||
// ---------------------------------------------------------
|
||
// 4. الإضافة في سيرفر التتبع (Tracking DB)
|
||
// ---------------------------------------------------------
|
||
|
||
$sqlRemote = "INSERT INTO `ride` (
|
||
`id`, `start_location`, `end_location`, `date`, `time`, `endtime`,
|
||
`price`, `passenger_id`, `driver_id`, `status`, `carType`,
|
||
`price_for_driver`, `price_for_passenger`, `distance`
|
||
) VALUES (
|
||
:id, :start_location, :end_location, :date, :time, :endtime,
|
||
:price, :passenger_id, :driver_id, :status, :carType,
|
||
:price_for_driver, :price_for_passenger, :distance
|
||
)";
|
||
|
||
// إضافة الـ ID للمصفوفة
|
||
$data[':id'] = $insertedId;
|
||
|
||
try {
|
||
error_log("🔄 [add_ride.php] Inserting into REMOTE DB...");
|
||
|
||
$stmtRemote = $con_ride->prepare($sqlRemote);
|
||
$stmtRemote->execute($data);
|
||
|
||
error_log("✅ [add_ride.php] Remote Insert Success.");
|
||
|
||
} catch (PDOException $eRemote) {
|
||
// نسجل خطأ الريموت لكن لا نوقف العملية لأن اللوكل تم بنجاح
|
||
error_log("⚠️ [add_ride.php] Remote DB Error: " . $eRemote->getMessage());
|
||
}
|
||
|
||
// طباعة النجاح (JSON صحيح)
|
||
jsonSuccess($insertedId);
|
||
|
||
} else {
|
||
error_log("❌ [add_ride.php] Failed to insert locally (Rows affected 0).");
|
||
jsonError("Failed to save ride information locally");
|
||
}
|
||
|
||
} catch (PDOException $e) {
|
||
// تسجيل الخطأ بدقة
|
||
error_log("❌ [add_ride.php] SQL Error: " . $e->getMessage());
|
||
jsonError("Database Error: " . $e->getMessage());
|
||
}
|
||
?>
|