<?php
include "db.php";

$car = $_POST['car'];
$id = $_POST['id'];

/*$query = mysqli_query($conn, "SELECT * FROM trips WHERE car = '$car' AND station_mileage > 0");
while($get = mysqli_fetch_array($query)){
  $update = mysqli_query($conn, "UPDATE trips SET end_mileage = '$get[station_mileage]' WHERE id = '$get[id]' ");
}*/
$query = mysqli_query($conn, "SELECT * FROM trips WHERE car = '$car' AND station_mileage > 0 ORDER BY id DESC");
while($trip = mysqli_fetch_array($query)){
  $query2 = mysqli_query($conn, "SELECT * FROM trips WHERE car = '$car' AND id > '$trip[id]' LIMIT 1");
  $nextid = mysqli_fetch_array($query2);
  
  $update = mysqli_query($conn, "UPDATE trips SET start_mileage = '$trip[station_mileage]' WHERE id = '$nextid[id]' ");
  $update2 = mysqli_query($conn, "UPDATE trips SET end_mileage = '$trip[station_mileage]' WHERE id = '$trip[id]' ");
  
}
?>