Skip to content
Snippets Groups Projects
Commit d03ed5ae authored by Vigneshwaran K's avatar Vigneshwaran K :first_quarter_moon:
Browse files

Upload New File

parent d0f94e9a
No related branches found
No related tags found
No related merge requests found
<?php
header("Content-Type: application/json");
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "crud_app";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die(json_encode(["error" => "Connection failed: " . $conn->connect_error]));
}
$method = $_SERVER["REQUEST_METHOD"];
if ($method == "GET") {
$result = $conn->query("SELECT * FROM users");
$users = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($users);
}
elseif ($method == "POST") {
$data = json_decode(file_get_contents("php://input"), true);
$stmt = $conn->prepare("INSERT INTO users (name, email) VALUES (?, ?)");
$stmt->bind_param("ss", $data['name'], $data['email']);
$stmt->execute();
echo json_encode(["message" => "User added successfully"]);
}
elseif ($method == "PUT") {
parse_str(file_get_contents("php://input"), $_PUT);
$id = $_GET['id'];
$data = json_decode(file_get_contents("php://input"), true);
$stmt = $conn->prepare("UPDATE users SET name=?, email=? WHERE id=?");
$stmt->bind_param("ssi", $data['name'], $data['email'], $id);
$stmt->execute();
echo json_encode(["message" => "User updated successfully"]);
}
elseif ($method == "DELETE") {
$id = $_GET['id'];
$stmt = $conn->prepare("DELETE FROM users WHERE id=?");
$stmt->bind_param("i", $id);
$stmt->execute();
echo json_encode(["message" => "User deleted successfully"]);
}
$conn->close();
?>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment