I have the following PHP code:
if(!empty($_POST)){
if( empty($_POST['usuario_rut_administrador'])
|| empty($_POST['nombre_estacionamiento'])
|| empty($_POST['cantidad_cupos'])
|| empty($_POST['latitud'])
|| empty($_POST['longitud']))
{
$response["success"]=0;
$response["message"]= "Por favor rellene todos los campos solicitados";
die(json_encode($response));
}
$query = "SELECT id_estacionamiento, usuario_rut_administrador, nombre_estacionamiento, cantidad_cupos, latitud, longitud from estacionamiento WHERE
id_estacionamiento=:idestacionamiento
and usuario_rut_administrador=:usuariorut
and nombre_estacionamiento=:nombreestacionamiento
and cantidad_cupos=:cantidadcupos
and latitud=:llatitud
and longitud=:llongitud";
$query_params=array(
':idestacionamiento'=> $_POST['id_estacionamiento'],
':usuariorut' => $_POST['usuario_rut_administrador'],
':nombreestacionamiento' => $_POST['nombre_estacionamiento'],
':cantidadcupos' => $_POST['cantidad_cupos'],
':llatitud' => $_POST['latitud'],
':llongitud' => $_POST['longitud']);
try{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error1. Please Try Again!";
die(json_encode($response));
}
$row = $stmt->fetch();
if ($row) {
$response["success"] = 0;
$response["message"] = "Lo siento, nombre o ubicacion ya existe";
die(json_encode($response));
}
$query ="INSERT into estacionamiento(usuario_rut_administrador, nombre_estacionamiento, cantidad_cupos, latitud, longitud)
VALUES (:usuariorut, :nombreestacionamiento, :cantidadcupos, :llatitud, :llongitud )";
$query_params=array(
':usuariorut' => $_POST['usuario_rut_administrador'],
':nombreestacionamiento' => $_POST['nombre_estacionamiento'],
':cantidadcupos' => $_POST['cantidad_cupos'],
':llatitud' => $_POST['latitud'],
':llongitud' => $_POST['longitud']);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
// solo para testing
//die("Failed to run query: " . $ex->getMessage());
$response["success"] = 0;
$response["message"] = "Error base de datos2. Porfavor vuelve a intentarlo";
die(json_encode($response));
}
$response["success"] = 1;
$response["message"] = "El estacionamiento se ha agregado correctamente";
echo json_encode($response);
$sql = "SELECT MAX(id_estacionamiento) FROM estacionamiento;";
$statement = $db->prepare($sql);
$statement->execute(); // no need to add '$sql' here, you can take that out
$item_id = $statement->fetchColumn();
echo $item_id;
$cantidadcupos=$_POST['cantidad_cupos'];
$idparking=$_POST['idestacionamiento'];
for($i=0; $i<$cantidadcupos;$i++){
$query="INSERT INTO cupos(numero, estado, reserva_id_reserva, estacionamiento_id_estacionamiento)
VALUES ('$cantidadcupos','disponible','1','$item_id')";
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
} catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Error base de datos2. Porfavor vuelve a intentarlo";
die(json_encode($response));
} } echo $cantidadcupos;
$response["success"] = 1;
$response["message"] = "111El cupo se ha agregado correctamente";
echo json_encode($response);
What I want to ask is the following:
The parking table has a field of number of places eg 20, when adding this data, in another table called spaces 20 records are created associated with the parking id that was added.
But I need to make the table quotas those 20 records are listed from 1 to 20, and then when you add another parking lot with your field number of quotas ex: 10, I also list them from 1 to 10. I hope it has it is understood. What method in php or query in mysql can help me with this?