React js POST method to two or more tables [closed]

0

I’m a newbie to react and have been trying to learn it by a random car project with crud operations. I got it working and all.. but now i want to add an option of asigning it to a person which probably would be a separate table with some data.

I’m also pretty new to PHP so i can’t wrap my mind around this problem 😀 I have a page where i display all the cars and also have a page where i display a single car with some info and posibility to “reserve” a car. Now i would like to reserve the car but with additional info like person that reserved it. So i would have to create a user when reserving. i’m not sure how to have two “POST” methods in the same file and reference one or another depending on if i’m registering a new car or registering a new user linked to the car. I’m using tanstack and axios if it helps in any way.. probably not 😀

I’m so sorry if i’m making no sense.. i haven’t programmed in a year so i’m trying to refresh some concepts.
This is my PHP File:

I tried using LEFT JOIN but and i got some info but im not sure how id be able to post into a table depending on which form i’m submiting.
I’ve seen some answers on here but mostly there are inserting in two different tables at the same time but i don’t need to insert into the second table if i’m only trying to register a car. But on more detailes page i would need to update two tables at the same time. I don’t understand how to proceed

error_reporting(E_ALL);
ini_set('display_errors',1);
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
header("Access-Control-Allow-Methods: *");

include 'DbConnect.php';
$objDb = new DbConnect;
$conn = $objDb -> connect ();

$method = $_SERVER['REQUEST_METHOD'];
switch ($method){

    case 'GET':
        $sql = "SELECT * FROM coches";
        $path = explode('/', $_SERVER['REQUEST_URI']);
      if(isset($path[3]) && is_numeric($path[3])){
        $sql .= " WHERE id = :id";
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':id', $path[3]);
        $stmt->execute();
        $coches = $stmt->fetch(PDO::FETCH_ASSOC);

      }else{
        $stmt = $conn->prepare($sql);
        $stmt->execute();
        $coches = $stmt->fetchAll(PDO::FETCH_ASSOC);
      }

      echo json_encode($coches);
      break;

    case 'POST':
        $coche = json_decode( file_get_contents('php://input'));
        $sql = "INSERT INTO coches(id,imagen,condicion,puertas,cv,km,marca,modelo,precio,color,detalles,anio,created_at) VALUES (null, :imagen, :condicion,:puertas,:cv,:km, :marca, :modelo, :precio, :color,:detalles,:anio, :created_at)";
        $stmt= $conn->prepare($sql);
        $created_at = date('Y-m-d');
        $stmt->bindParam(':imagen',$coche->imagen);
        $stmt->bindParam(':condicion',$coche->condicion);
        $stmt->bindParam(':puertas',$coche->puertas);
        $stmt->bindParam(':km',$coche->km);
        $stmt->bindParam(':cv',$coche->cv);
        $stmt->bindParam(':marca',$coche->marca);
        $stmt->bindParam(':modelo',$coche->modelo);
        $stmt->bindParam(':precio',$coche->precio);
        $stmt->bindParam(':color',$coche->color);
        $stmt->bindParam(':detalles',$coche->detalles);
        $stmt->bindParam(':anio',$coche->anio);
        $stmt->bindParam(':created_at',$coche->created_at);
        if($stmt -> execute()){
            $response = ['status' => 1, 'message' => 'El coche se ha registrado correctamente'];
        }
        else{
            $response = ['status' => 0, 'message' => 'No se ha registrado'];
        }
        echo json_encode($response);
        break;

    case "PUT":
        $coche = json_decode( file_get_contents('php://input') );
        $sql = "UPDATE coches SET condicion= :condicion,puertas= :puertas,km= :km,cv= :cv,marca= :marca, modelo =:modelo, precio =:precio, color=:color,detalles=:detalles,anio=:anio, updated_at =:updated_at WHERE id = :id";
        $stmt = $conn->prepare($sql);
        $updated_at = date('Y-m-d');
        $stmt->bindParam(':id', $coche->id);
        $stmt->bindParam(':marca', $coche->marca);
        $stmt->bindParam(':modelo', $coche->modelo);
        $stmt->bindParam(':cv', $coche->cv);
        $stmt->bindParam(':km', $coche->km);
        $stmt->bindParam(':puertas', $coche->puertas);
        $stmt->bindParam(':condicion', $coche->condicion);
        $stmt->bindParam(':precio', $coche->precio);
        $stmt->bindParam(':color', $coche->color);
        $stmt->bindParam(':detalles', $coche->detalles);
        $stmt->bindParam(':anio', $coche->anio);
        $stmt->bindParam(':updated_at', $updated_at);

        if($stmt->execute()) {
            $response = ['status' => 1, 'message' => 'Los datos del coche ha sido actualizado.'];
        } else {
            $response = ['status' => 0, 'message' => 'Los datos no se han actualizado'];
        }
        echo json_encode($response);
        break;
    
    case "DELETE":
        $sql = "DELETE FROM coches WHERE id= :id";
        $path = explode('/', $_SERVER['REQUEST_URI']);
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':id', $path[3]);
        $stmt->execute();
        break;
}