How to Build a Basic CRUD App with PHP?
In this guide, you'll learn how to develop a complete PHP application that performs Create, Read, Update, and Delete (CRUD) operations.
By leveraging the Parse PHP SDK with your Back4app project, you'll be able to seamlessly manage data and integrate robust backend features into your PHP app.
This tutorial covers everything from initializing your project and designing your data model to building secure PHP scripts for handling CRUD operations.
This comprehensive walkthrough ensures that you create a production-ready application with secure user authentication, efficient data management, and an intuitive admin interface.
- Understand how to construct CRUD operations using PHP alongside a backend service.
- Acquire hands-on experience in designing a scalable data model.
- Learn how to utilize an intuitive admin panel to manage your backend effortlessly.
- Get acquainted with deployment strategies including containerization for your PHP application.
Before you begin, ensure you have the following:
- A Back4app account with a new project set up. Visit Getting Started with Back4app if you need assistance.
- A PHP development environment. Ensure you have PHP 7.4 or later installed along with a suitable web server setup.
- Log in to your Back4app account.
- Click the “New App” button on your dashboard.
- Name your project: Basic-CRUD-App-PHP and follow the instructions to create it.

Once created, your new project will be visible on your dashboard, serving as the backbone for your PHP CRUD application.
For this application, you'll define several classes directly in your backend. The examples below illustrate the classes and fields you should set up using the Back4app dashboard.
This class holds details for every item.
Field | Type | Explanation |
---|---|---|
_id | ObjectId | Auto-generated unique identifier. |
title | String | The name of the item. |
description | String | A short overview of the item. |
created_at | Date | Time when the item was created. |
updated_at | Date | Time when the item was last modified. |
This class manages user details and authentication.
Field | Type | Explanation |
---|---|---|
_id | ObjectId | Auto-generated unique identifier. |
username | String | A unique identifier for the user. |
String | The user's email address. | |
password_hash | String | Securely stored hashed password. |
created_at | Date | Timestamp when the account was created. |
updated_at | Date | Timestamp for the latest account update. |
You can manually create these classes in your Back4app dashboard by adding new classes and specifying the appropriate fields.

Simply choose the desired data type, set the field name, and determine whether it’s required or has a default value.

The Back4app Admin App provides an easy-to-use, drag-and-drop interface for managing your backend data. This tool lets you perform CRUD operations effortlessly without extra code.
- Navigate to the “More” menu in your Back4app dashboard.
- Select “Admin App” and then click “Enable Admin App.”
- Configure your admin account by creating your first administrative user, which also sets up default roles.

After activation, log into the Admin App to manage your classes and execute CRUD operations.

Within this panel you can:
- Add New Records: Insert new entries into classes like Items.
- View and Modify Records: Click on a record to see its details or update its fields.
- Delete Records: Remove records that are no longer required.
This intuitive interface greatly simplifies the task of managing your backend data.
Now that your backend is configured, it's time to link your PHP application using the Parse PHP SDK.
Install the Parse PHP SDK: Use Composer to add the SDK to your project:
Set Up Parse in Your PHP Application: Create a configuration file (e.g., parseConfig.php):
This script retrieves all records from the Items class and displays them.
If the Parse SDK is not an option, you can perform CRUD actions via REST. For instance, to fetch items using PHP and cURL:
Integrate these code snippets within your PHP application as needed.
Enhance your application's security by setting ACLs on objects. The following example demonstrates how to create a private item:
Within the Back4app dashboard, adjust the CLPs for each class so that only authorized users have access to sensitive data.
Back4app uses Parse’s built-in User class for authentication. The example below shows how to handle user signup in PHP:
You can create similar scripts for login and session management. Additional features such as social authentication and email verification can also be set up via the Back4app dashboard.
Back4app’s Web Deployment service supports PHP applications as well. Follow these steps to deploy your project:
Ensure your project directory is structured like this:
Initialize a Git repository in your project folder:
Stage your files:
Commit your changes:
- Create a GitHub repository: For instance, name it Basic-CRUD-App-PHP.
Push your code to GitHub:
- Access Web Deployment: Log in to your Back4app dashboard, go to your project, and click on Web Deployment.
- Connect Your GitHub Account: Follow the prompts to link your GitHub repository.
- Select Your Repository and Branch: Choose Basic-CRUD-App-PHP and the main branch where your PHP code resides.
Set up your deployment configuration with:
- Build Command: If necessary, specify a command to prepare your application.
- Output Directory: Point to the folder (e.g., public/) that serves as your document root.
- Environment Variables: Add any required API keys or configuration settings.
If you prefer containerization, add a Dockerfile to your repository:
Configure Back4app to deploy your containerized application if desired.
- Click the Deploy Button: Initiate deployment once all configurations are complete.
- Monitor the Deployment Process: Back4app will pull your repository, run any build commands, and deploy your PHP application.
- Access Your Application URL: After deployment, a URL will be provided where your app is live.
- Visit the Provided URL: Open the URL in your browser to confirm your PHP application is running.
- Test CRUD Functionality: Ensure that all create, read, update, and delete operations perform as expected.
- Troubleshoot if Needed: Use browser developer tools or check Back4app logs for any issues.
Congratulations! You have successfully built a PHP-based CRUD application using Back4app. Throughout this tutorial, you:
- Set up a project named Basic-CRUD-App-PHP.
- Designed clear classes for Items and Users.
- Utilized the Admin App for seamless data management.
- Connected your PHP scripts to the backend using the Parse PHP SDK.
- Secured your data with ACLs and implemented user authentication.
- Enhance the User Interface: Expand your PHP application with features like detailed item views, search functionality, or real-time updates.
- Integrate Advanced Features: Consider adding cloud functions, third-party API integrations, or role-based access control.
- Deepen Your Knowledge: Explore the Back4app documentation and other resources to further enhance your development skills.
By following this tutorial, you are now equipped to develop robust PHP applications that efficiently manage data. Happy coding!
