Хочу добавить новую строку через postman но не получается вот код
<?php
function getPosts($connect) {
$posts = mysqli_query($connect, 'SELECT * FROM `auto`');
$postsList = [];
while ($post = mysqli_fetch_assoc($posts)) {
$postsList[] = $post;
}
return $postsList;
}
function getMen($connect) {
$men = mysqli_query($connect, 'SELECT * FROM `Men`');
$menList = [];
while ($man = mysqli_fetch_assoc($men)) {
$menList[] = $man;
}
return $menList;
}
function getPost($connect, $id) {
$post = mysqli_query($connect, "SELECT * FROM `auto` WHERE `id` = '$id'");
$post = mysqli_fetch_assoc($post);
echo json_encode($post);
}
function getMenPost($connect, $id) {
$menPost = mysqli_query($connect, "SELECT * FROM `Men` WHERE `id` = '$id'");
$menPost = mysqli_fetch_assoc($menPost);
echo json_encode($menPost);
}
function addPost($connect, $data) {
$brand = $data['brand'];
$model = $data['model'];
$bodyColor = $data['body color'];
$stateNumber = $data['StatenumberoftheRussianFederation'];
$status = $data['Status'];
$FullName = $data['FullName'];
$Gender = $data['Gender'];
$Phone = $data['Phone'];
$Address = $data['Address'];
$Auto = $data['Auto'];
mysqli_query($connect, "INSERT`auto` (`brand`, `model`, `body color`, `StatenumberoftheRussianFederation`, `Status`) VALUES ('$brand', '$model', '$bodyColor', '$stateNumber', '$status')");
$autoId = mysqli_insert_id($connect);
mysqli_query($connect, "INSERT`Men` (`FullName`, `Gender`, `Phone`, `Address`, `Auto`) VALUES ('$fullName', '$gender', '$phone', '$address', '$auto')");
if ($autoId > 0) {
http_response_code(201);
$res = [
'status' => true,
'post_id' => $autoId
];
} else {
http_response_code(500);
$res = [
'status' => false,
'message' => 'Failed to add post to the database.'
];
}
echo json_encode($res);
}
<?php
header('Content-Type: application/json');
require 'connect.php';
require 'functions.php';
$method = $_SERVER['REQUEST_METHOD'];
$type = $_GET['q'];
$params = explode('/', $type);
$type = $params[0];
$id = $params[1];
if ($method === 'GET') {
if ($type === 'posts') {
if (isset($id)) {
getPost($connect, $id);
} else {
$posts = getPosts($connect);
echo json_encode($posts);
}
} elseif ($type === 'men') {
if (isset($id)) {
getMenPost($connect, $id);
} else {
$men = getMen($connect);
echo json_encode($men);
}
} else {
echo "Invalid type specified.";
}
} elseif ($method === 'POST') {
if ($type === 'posts') {
addPost($connect, $_POST);
}
}