The vision of this project is to empower developers to perform basic CRUD operations effortlessly without the need to write custom PHP code. The scripts are designed to be dynamic, allowing users to interact with the API simply by understanding the provided documentation and making HTTP requests.
- Introduction
- Connection Configuration
- Create Data (
create.php
) - Read Data (
read.php
) - Update Data (
update.php
) - Delete Data (
delete.php
) - File Upload (
file-upload.php
) - Customization and Security
- Credits, Message from Creator
This documentation provides an overview of a set of PHP scripts designed for basic CRUD operations (Create, Read, Update, Delete) and file uploads. The scripts are intended to be used as an API for managing data in a relational database.
This project provides a set of PHP scripts designed to serve as a simple API for performing CRUD (Create, Read, Update, Delete) operations on a relational database. Additionally, it includes a script for handling file uploads. The scripts are intended to offer a flexible and extensible solution for managing data interactions with a backend server.
Before using the scripts, ensure that the database connection is properly configured. The connection details can be found in the configuration/connection.php
file. Modify the file to set your database host, username, password, and database name.
Example configuration/connection.php
:
define('DB_SERVER', 'YOUR_HOSTNAME');
define('DB_USERNAME', 'YOUR_USERNAME');
define('DB_PASSWORD', 'YOUR_PASSWORD');
define('DB_NAME', 'YOUR_DATABASE');
Description: This script handles the creation of new records in the database based on the provided JSON data.
table
(required): The name of the database table to insert data into.validation
(optional): Validation rules for the data.data
(required): An array of records to be inserted.
The validation
is used to validate the data based on the provided rules. The following validation rules are available:
required
: The field must not be empty.string
: The field must be a valid string.name
: The field must be a valid name and without any integer or special characters.email
: The field must be a valid email format.numeric
: The field must be numeric.min-length:X
: The field must be at least X characters long.max-length:X
: The field must be at most X characters long.length:X
: The field must be exactly X characters long.unique
: The field value must be unique within the specified table | It'll check theid
while updating the record, if it the same then simply it'll update the column.
{
"table": "users",
"validation": [
{
"name": "required|string",
"email": "required|email|unique",
"phone": "required|numeric|unique|length:10",
"password": "required|min-length:6",
"age": "optional|numeric"
}
],
"data": [
{
"name": "Imdadullah",
"email": "imdad@imdos.com",
"phone": "9992229990",
"password": "VerySecurePassword",
"age": 22
}
]
}
Description: This script retrieves data from the database based on the specified parameters.
table
(required): The name of the database table to query.select
(required): An array of columns to select. Default is all columns (*).join
(optional): An array of join clauses for performing joins.conditions
(optional): An array of conditions with object withon
,type
andvalue
for filtering data.rawConditions
(optional): An array of raw conditions for filtering data, make you sure pass eitherconditions
orrawConditions
.order
(optional): An object withon
andtype
for ordering data.limit
(optional): Limit the number of records returned.
{
"table": "users",
"select": ["id", "name", "email", "age"],
"order": { "on": "id", "type": "DESC" },
"conditions": [
{
"on": "age",
"type": ">=",
"value": "18"
},
{
"on": "status",
"type": "=",
"value": "active"
},
{
"on": "email",
"type": "LIKE",
"value": "@gmail.com%"
}
],
"limit": 10
}
{
"table": "users",
"select": ["id", "name", "email", "age"],
"order": { "on": "id", "type": "DESC" },
"rawConditions": [
"WHERE age >= '18' OR type = 'customer' AND status = 'active'"
]
}
Note: You'll have to mention each table name along with the column name to use JOIN
{
"table": "users",
"select": [
"users.name",
"users.email",
"items.title",
"items.price",
"purchases.amount",
"purchases.created_at AS purchased_date"
],
"conditions": [
{
"on": "purchases.item_id",
"type": "=",
"value": "102"
}
],
"join": [
{
"table": "purchases",
"on": ["purchases.user_id", "users.id"],
"type": "LEFT"
},
{
"table": "items",
"on": ["items.id", "purchases.item_id"],
"type": "LEFT"
}
]
}
Description: This script updates existing records in the database based on the specified parameters.
table
(required): The name of the database table to query.data
(required): An array of fields and values to be updated.conditions
(required): An array of conditions for identifying records to update.validation
(optional): Validation rules for the data.
{
"table": "users",
"data": [
{
"name": "Imdadullah Babu",
"age": 22
}
],
"validation": [
{
"name": "required|string",
"age": "optional|numeric"
}
],
"conditions": [
{
"on": "id",
"type": "=",
"value": "1"
}
]
}
Description: This script deletes records from the database based on the specified parameters.
table
(required): The name of the database table to delete from.conditions
(required): An array of conditions for identifying records to delete.
{
"table": "users",
"conditions": [
{
"on": "id",
"type": "=",
"value": "1"
}
]
}
Description: This script handles the uploading of files to a specified destination.
fileDestination
(required): The directory where the files will be stored.fileValidation
(required): A comma-separated list of allowed file extensions.
Note: You can upload multiple files at once, and this will not insert into your database table, You'll get the url as response and you can save it to the database.
<script>
const uploadButton = document.getElementById("button");
uploadButton.addEventListener("click", async function () {
const fileInput = document.getElementById("file");
const imageInput = document.getElementById("image");
const file = fileInput.files[0];
const image = imageInput.files[0];
const formData = new FormData();
formData.append("file", file);
formData.append("image", image);
formData.append("fileDestination", "files"); // Files will be uploaded to the mentioned destination on the 'uploaded' directory
formData.append("fileValidation", ["jpg", "png", "pdf"]);
const request = await fetch("YOUR_API_REQUEST_ENDPOINT", {
method: "POST",
body: formData,
});
const response = await request.json();
console.log(response);
});
</script>
{
"file": "uploads/files/2024011984368.jpg",
"image": "uploads/files/2024011960039.jpg"
}
Note: Customize and extend the scripts based on your project's requirements. Ensure proper validation and security measures are implemented in a production environment.
For additional security measures or custom logic, developers can extend the functionality by adding their own logic to the configuration/custom-functions.php
file. This provides a space to incorporate token validation, custom authentication, or any other security measures according to project requirements.
- Open the configuration/custom-functions.php file.
- Add your own custom logic, functions, or security measures.
- Use the added functions within the codebase as needed.
- Developers are encouraged to review and customize the provided scripts to meet the specific requirements of their projects.
- Custom functions added to custom-functions.php can be seamlessly integrated into the existing codebase.
- Developers have the freedom to enhance security and implement project-specific logic according to their needs.
This project was developed by Imdadullah Babu, The scripts aim to provide a foundation for PHP developers to integrate basic database operations and file uploads into their projects without writing repeatable codes.
This project is open-source, and we welcome contributions from developers worldwide. Whether you're interested in adding new features, improving documentation, fixing bugs, or suggesting enhancements, your contributions are valuable.
Together, we can make this project even more versatile and beneficial for the developer community. Your contributions, big or small, are highly appreciated.
Thank you for being a part of our open-source journey!