Unlock Your Data: A Beginner’s Guide to PHP MySQL Connect & CRUD Operations

Welcome back to the exciting world of web development! You’ve likely heard of dynamic websites and the power of data. Our previous tutorial introduced you to the concept of form handling and variables enabling dynamic messages shown to the user, In this tutorial we will take a significant leap forward. This time, we’re not just connecting; we’re building a practical, very simple CRUD application that will allow you to Create, Read, Update, and Delete data directly from your web browser.

This guide is designed to be a hands-on PHP database tutorial, focusing on the absolute basics of PHP MySQL connect and fundamental CRUD operations without getting bogged down in complex error handling or advanced features. We’ll set up a simple table, display its contents, and then create straightforward ways to interact with that data.

Setting the Stage: Database and Connection

Before we write any PHP, let’s prepare our database. We’ll create a simple users table to store some basic information.

1. Create Your Database Table

If you are using Laragon as suggested in the previous tutorials – when you create a simple ‘Blank’ project, it will create a MySQL database with the same name as your project as default, however you still need to create ‘Tables’ within this to be able to use it.

So lets start a new project – right click on the Laragon icon in your task tray, and select ‘Quick app > Blank’. In the popup box, enter a name for your project – for this tutorial I went with: simple-crud-db

Laragon utilises HeidiSQL for database management, which it makes it very easy to manage rather than controlling databases through the commandline. In the same Laragon tray menu, when you click on ‘Tools > HeidiSQL’ button you will be presented with the below:

All of these settings should already be pre-set, and all you need to do is click ‘Open’

Once opened you should see the name of your newly created project as a database entry in the LH side panel. There is currently no tables in it, so it will look a bit blank. You can utilise some of the GUI buttons and tools to create a new table, but I prefer to instead do this through SQL code.

In the main window area, there should be a tab called ‘Query’ – this is used to execute SQL code to interact with the database. We want to create a new users table within the database, so simply copy and paste the below, and then hit the Execute button in the menu above (looks like a play button)

Once completed, you may need to click the Refresh button in the menu (or F5 key), and all being well, you should be able to see a new table appear under the database heading:

2. Connect your website code to the database

The next step is to link your website pages and PHP files to the database

To keep our code clean, let’s create a separate file config.php in the main root folder that will handle our database connection. This file will be included in all our other PHP scripts. Copy the below and paste into the file and save it

Remember to update the $username and $password fields with your actual MySQL credentials. In Laragon the user is root with an empty password as default.

Building Our Simple PHP CRUD Application (index.php)

Now, let’s build index.php, which will display our data and allow us to create new records. This will be the main entry point for our PHP database tutorial example.

This index.php file handles two main things: reading all existing users from the database and displaying them in a table, and providing a simple form to create new users. When the form is submitted, the data is inserted, and the page redirects to itself to show the updated list.

Updating Data (edit.php)

To handle updating records, we’ll create edit.php. This page will fetch a specific user’s data based on an id passed in the URL, display it in a pre-filled form, and then process the update.

Deleting Data (delete.php)

Finally, for deleting records, we’ll create a simple delete.php script that takes an id from the URL, performs the deletion, and then redirects back to the main page.

Conclusion: Your First PHP CRUD Application is Ready!

Congratulations! You’ve just built a fully functional, albeit basic, PHP CRUD application. You’ve gone beyond just a simple PHP MySQL connect and created a system to manage data effectively: creating new entries, reading existing ones, updating information, and deleting records. This foundational understanding is crucial for almost any dynamic web project.

All being well, if you now visit the address Laragon has created for your site (likely to be something like http://simple-crud-db.test/ ) – then the below form should show in your browser.

Go ahead and play around with parts of the code and see what happens – you have nothing to lose!

While this example intentionally skipped robust error handling and advanced UI, it provides a solid starting point for your PHP database tutorial journey. From here, you can explore adding more features, improving security, enhancing user experience with JavaScript modals, and integrating more complex database designs. Keep experimenting and building – the possibilities with PHP and MySQL are vast!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *