Files
Siro/loction_server/siro/ride/location/update.php
2026-06-29 23:09:43 +03:00

65 lines
2.3 KiB
PHP

<?php
include "../../connect.php";
$driver_id = filterRequest("driver_id");
$latitude = filterRequest("latitude");
$longitude = filterRequest("longitude");
$status = filterRequest("status");
$heading = filterRequest("heading");
$speed = filterRequest("speed");
$distance = filterRequest("distance");
// 1. قمنا بحذف السطر التالي لأنه مصدر المشكلة
// $updated_at = date("Y-m-d H:i:s");
// Basic validation
if (!$driver_id || !$latitude || !$longitude || $status === null) {
http_response_code(400);
printFailure('Missing required fields');
exit;
}
// Secure SQL using prepared statement
// 2. لاحظ التغيير داخل جملة SQL
// بدلنا :updated_at بكلمة NOW() وهي دالة في قاعدة البيانات
$sql = "INSERT INTO `car_locations` (
`driver_id`, `latitude`, `longitude`, `heading`, `speed`, `distance`, `status`, `updated_at`
) VALUES (
:driver_id, :latitude, :longitude, :heading, :speed, :distance, :status, NOW()
)
ON DUPLICATE KEY UPDATE
`latitude` = VALUES(`latitude`),
`longitude` = VALUES(`longitude`),
`heading` = VALUES(`heading`),
`speed` = VALUES(`speed`),
`distance` = VALUES(`distance`),
`status` = VALUES(`status`),
`updated_at` = NOW()"; // وهنا أيضاً جعلنا التحديث يأخذ وقت السيرفر مباشرة
try {
$stmt = $con->prepare($sql);
// The execute method returns true on success and false on failure.
$success = $stmt->execute([
':latitude' => $latitude,
':longitude' => $longitude,
':heading' => $heading,
':speed' => $speed,
':distance' => $distance,
':status' => $status,
// ':updated_at' => $updated_at, <-- قمنا بحذف هذا السطر من المصفوفة لأنه لم يعد موجوداً في الاستعلام
':driver_id' => $driver_id
]);
if ($success) {
printSuccess("Car location updated successfully");
} else {
printFailure("Failed to update car location");
}
} catch (PDOException $e) {
http_response_code(500);
printFailure('Database error occurred');
}
?>