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;
}
Annie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.