Insert edit and delete using PHP and MySQL

Insert edit and delete using php and mysql

How to Insert edit and delete using PHP and MySQL from Database

Introduction of PHP PDO

PHP is an open-source scripting language, which is extensively used for creating interactive, dynamic and user-friendly web pages. With the help PHP PDO, we can access a database such as MYSQL, SQLite etc. The PHP’s latest (5.1) version provided a new database connection abstraction library, which is PHP PDO (PHP Data Objects).

What is PHP PDO?

PDO stands for PHP Data Object, a PHP extension that defines a light-weight and consistent interface for accessing a database in PHP. It’s a set of PHP extensions that offer a PDO class and database driver. For more information please visit the PHP PDO official website.

In this tutorial, we can learn how to create Insert edit and delete using PHP and MySQL. This tutorial is a second part of the Registration and Login Form Free Download with PHP and MySQL. In the first tutorial, I explain how to create a responsively Registration, login, and admin dashboard using PHP and MySQL. If you want to learn how to create a Registration and Login Form Free Download with PHP and Mysql. Please visit the PHP category in the sidebar.

1. Getting Started

1.1 Requirements

  1. Dreamweaver/Notepad++
  2. Wamp server
  3. MySQL database
  4. PHP PDO Extension
  5. Bootstrap

1.2 File Structure & Setup

Insert edit and delete using PHP and MySQL folder structure

Create the below folder structure for your project setup.

Path : C:/wamp64/www/Insert edit and delete using PHP and MySQL

1.3 What We Will Learn in this Tutorial

Before starting the programming we need to create Insert edit and delete using PHP and MySQL, the below pages in the project folder directory.

Create page-top.php, This page is used to add all the javascript and CSS for the page design.

page-bottom.php : Paste the below code on page-bottom.php

  1. Create a database table
  2. Create index.php page
  3. Create addRecord.php page
  4. Create common.php
  5. Create deleteRecord.php
  6. Create editRecord.php
  7. Create editImage.php
  8. Create downloadCsv.php
  9. Create dbconfig.php

1. Create Database Table for Records

Create the below Database table in MySQL to store the record and retrieve them with PHP and MySQL. Execute the following MySQL query.

In this tutorial, I am using Bootstrap to design the button and user interface, So you can download the bootstrap package from the official website.

2. Create index.php page to display the record

display the record from the Database using PHP and MySQL

Create index.php to display the record from the Database using PHP and MySQL. Copy and paste the following code and place it on the index.php

3. Create addRecord.php page

insert data in the database using PHP and MySQL

Here I using Bootstrap to design the HTML form and taking four input fields and one input type file field to allow to pick the file and upload it in the server directory path.

The form <form> tag contain two attributes.

1. Method (method=”post”): It should send the form data to the server for record insert into the database using the HTTP POST method.

2. enctype=”multipart/form-data”, Enctype is the type of encoding that specifies the form-data needs to be encoded as MIME multipart when submitting the form.

Paste the following code in addRecord.php to create a modal popup form to insert data in the database using PHP and MySQL.

In the PHP PDO it is easy to upload documents in the server directory path and also easy to store the directory path in the MySQL database.

You can easily upload pdf, word, jpg, gif, videos, and much more. With the help of PHP PDO, we can easily restrict the file extension while uploading the documents on the server.

Sweet alert after data inserted in mysql

4. Create a common.php

Create common.php to write the PHP PDO code on it. This file will include functions that we will execute in all our PHP and MySQL, that is so we do not have to write down the same code in each PHP file.

5. Create deleteRecord.php

delete the record from the database

This page will be used to delete the record from the database on Id wise.

Paste the following on deleteRecord.php file.

record  deleted successfully in php mysql

6. Create editRecord.php

editRecord.php is used to update/edit the record on the database table. this page is similar to addRecord.php. But instead of entering the new record, it will auto-populate the existing data from the database table using PHP and MySQL.

update/edit the record on the database table

7. Create editImage.php

This page is used to replace the existing images from the images directory and also update the image URL path in the database.

Update image using PHP PDO
After Image update

8. Create downloadCsv.php

downloadCsv.php this is used to fetch the record and download the data in a CSV file using PHP and MySQL.

fetch the record and download the data in a CSV

Paste the following PHP PDO code in the downloadCsv.php file.

9. Create dbconfig.php

This function will connect to the database. copy and paste the below code and paste it on dbconfig.php to connect the database. Below the PHP PDO code to open the connection between the database wrap with try/catch using PHP PDO extension.

Conclusion

Congratulations! You’ve successfully created an Insert edit and delete using PHP and MySQL. You can modify this code as per your requirement

If you enjoyed this tutorial so please do not forget to share on social media and take a look at our many extra tutorials on our website.