React js POST method to two or more tables

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 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 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 for the CRUD:

<?php
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;
}

New contributor

Annie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật