This project demonstrates a simple CRUD (Create, Read, Update, Delete) API using PHP. The front-end is styled with basic CSS and the API can be tested with Postman or the front-end file (index.html) of the project.
- Create: Add a new user.
- Read: Fetch all users.
- Update: Modify existing user details.
- Delete: Remove a user.
- HTML
- CSS
- JavaScript (Fetch API)
- PHP
- Postman / Browser for API testing
Clone the repository:
git clone https://github.com/vsbuidev/php-api.git cd php-api/rest-apiDatabase Configuration:
- Create a MySQL database named
crud_api. - Import the following SQL script to create the
userstable:CREATETABLE `users` ( `id`int(11) NOT NULL AUTO_INCREMENT, `name`varchar(50) NOT NULL, `email`varchar(50) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- Create a MySQL database named
Update Database Configuration in
db.php:<?phpclass Database { private$host = "localhost"; private$db_name = "crud_api"; private$username = "root"; private$password = ""; public$conn; publicfunctiongetConnection(){ $this->conn = null; try { $this->conn = newPDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password); $this->conn->exec("set names utf8"); } catch(PDOException$exception) { echo"Connection error: " . $exception->getMessage(); } return$this->conn; } } ?>
Create User:
- URL:
http://localhost/crud_api/api/create.php - Method:
POST - Request Body:
{ "name": "User Name", "email": "user@example.com" }
- URL:
Read Users:
- URL:
http://localhost/crud_api/api/read.php - Method:
GET
- URL:
Update User:
- URL:
http://localhost/crud_api/api/update.php - Method:
PUT - Request Body:
{ "id": 1, "name": "Updated Name", "email": "updated@example.com" }
- URL:
Delete User:
- URL:
http://localhost/crud_api/api/delete.php - Method:
DELETE - Request Body:
{ "id": 1 }
- URL:
Open index.html in your browser. This file provides a simple interface to test the API.
Basic CSS is included to style the front-end form elements and layout.
- Import the provided API endpoints into Postman.
- Test each endpoint by sending appropriate requests (as mentioned above).
This project is just for educational purposes only.