Add Data

Very Simple Add, Edit, Delete, View (CRUD) in PHP & MySQL [Beginner Tutorial]

In this article, I will be presenting simple PHP & MySQL code to add, edit, delete and view data. This kind of system is also referred to CRUD (Create, Read, Update, Delete).

Here is a step-by-step guide on creating a CRUD system using PHP & MySQL:

First of all, we will create a new MySQL database. Let us name the database as ‘test‘.

Then, we will create a new table in database ‘test’. Let us name the table as ‘users‘.

Читайте также:  Decode the string in java

Now, we will create a config.php file which contains database connection code. This code connects to the MySQL database. This file is included in all PHP pages where database connection is necessary.

dbConnection.php

In below code, the database host name is localhost where username=root and password=root . The database test has been selected.

To add data to the database, we need an html form.

     

Add Data

Home

Name Age Email

Form action on add.php is addAction.php . It means that the submitted form data will go to addAction.php .

In addAction.php , we do a simple validation of checking if the entered name, email & age are empty or not. If they are all filled then the data will be inserted into database table.

addAction.php

  Name field is empty.
"; > if (empty($age)) < echo "Age field is empty.
"; > if (empty($email)) < echo "Email field is empty.
"; > // Show link to the previous page echo "
Go Back"; > else < // If all the fields are filled (not empty) // Insert data into database $result = mysqli_query($mysqli, "INSERT INTO users (`name`, `age`, `email`) VALUES ('$name', '$age', '$email')"); // Display success message echo "

Data added successfully!

"; echo "View Result"; > > ?>

Data from database is fetched and displayed in index.php file. This is our homepage. This file also contains a link to add data. On every row of displayed data, there is also a link to edit and delete data. Below is a sample image of our homepage:

crud php

     

Homepage

Add New Data

"; echo ""; echo ""; echo ""; echo ""; > ?>
Name Age Email Action
".$res['name']."".$res['age']."".$res['email']."Edit | Delete

Each row of data can be edited separately. Row ID is passed in the URL of edit.php . ID uniquely identifies the data entry.

The action of the form in edit.php is editAction.php . It means that the submitted form data will go to editAction.php .

In edit.php , a single row entry of data is fetched based on the id . The fetched data is displayed in the edit form. When user edits the data and submits the form, the submitted form data goes to editAction.php .

     

Edit Data

Home

Name ">
Age ">
Email ">
>

In editAction.php some simple validation is done for empty data. When everything is correct, then that particular entry of data is updated in the database.

editAction.php

Each row of data can be deleted separately. Row ID is passed in the URL of delete.php . ID uniquely identifies the data entry. After deletion, the user is redirected to homepage ( index.php ).

Источник

Insert, View, Edit and Delete Record from Database Using PHP and MySQLi

In this tutorial, I will explain how to insert, view, edit and delete record from database using PHP and Mysqli, basically this tutorial is a second part of Simple User Registration & Login Script in PHP and MySQLi, in this first part I explained how to create simple user registration and login using PHP and MySQLi, if you do not know how to create user registration and user login form so kindly first check this tutorial Simple User Registration & Login Script in PHP and MySQLi, after that now come back to this tutorial.

I have tested this tutorial code from PHP 5.6 to PHP 8.2 versions, it is working smoothly on all PHP versions. However, I would recommend to use the latest PHP version.

I would suggest you to check PHP CRUD Operations Using PDO Prepared Statements, as this is compatible with PHP 7 and PHP 8 both versions.

Steps to Creating an Insert, View, Edit and Delete Record from Database Using PHP and MySQLi

Now I am assuming that you have already created user registration and login forms which I created in Simple User Registration & Login Script in PHP and MySQLi now I will create another table to keeping records in it, update dashboard.php file and add four more new pages which are insert.php, view.php, edit.php, and delete.php, follow the following steps:

  1. Create Another Table for Records
  2. Update Dashboard File
  3. Create Insert Page
  4. Create View Page
  5. Create Edit/Update Page
  6. Create Delete Page

1. Create Another Table for Records

Execute the below SQL query:

CREATE TABLE IF NOT EXISTS `new_record` ( `id` int(11) NOT NULL AUTO_INCREMENT, `trn_date` datetime NOT NULL, `name` varchar(50) NOT NULL, `age`int(11) NOT NULL, `submittedby` varchar(50) NOT NULL, PRIMARY KEY (`id`) );

2. Update Dashboard Page

Update dashboard.php file and paste the following code in it.

        

Welcome to Dashboard.

Home

Insert New Record

View Records

Logout

3. Create Insert Page

Create a page with name insert.php and paste the below code in it.

 ?>       

Dashboard | View Records | Logout

Insert New Record

insert-new-record

4. Create View Page

Create a page with name view.php and paste the below code in it.

        

Home | Insert New Record | Logout

View Records

?>
S.No Name Age Edit Delete
">Edit ">Delete

view-record

5. Create Edit/Update Page

Create a page with name edit.php and paste the below code in it.

        

Dashboard | Insert New Record | Logout

Update Record

'.$status.''; >else < ?>
?>

update-record

6. Create Delete Page

Create a page with name delete.php and paste the below code in it.

This tutorial is only to explain the basic concept of PHP CRUD operations, you will need to implement more security when working with live or production environment.

If you found this tutorial helpful so share it with your friends, developer groups and leave your comment.

Facebook Official Page: All PHP Tricks

Twitter Official Page: All PHP Tricks

Javed Ur Rehman is a passionate blogger and web developer, he loves to share web development tutorials and blogging tips. He usually writes about HTML, CSS, JavaScript, Jquery, Ajax, PHP and MySQL.

Источник

Tutorial: Easy and Simple Add, Edit, Delete MySQL Table Rows in PHP with Source Code

add edit delete in php

In this tutorial, I will show you how to create a simple add, edit and delete functions in MySQL Table Rows using PHP with MySQLi database. This tutorial does not include a good design but will give you an idea of the essential tasks in PHP and basic queries in MySQLi.

Creating a Database

First, we’re going to create a database that contains our data.

2. Click databases, create a database and name it as “basic_command”.

3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.

CREATE TABLE `user` ( `userid` INT(11) NOT NULL AUTO_INCREMENT, `firstname` VARCHAR(30) NOT NULL, `lastname` VARCHAR(30) NOT NULL, PRIMARY KEY (`userid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

add edit delete in php

Creating a Connection

Next step is to create a database connection and save it as “conn.php”. This file will serve as our bridge between our form and our database. To create the file, open your HTML code editor and paste the code below after the tag.

Creating a Table and Add Form

Next step is to create our table and add form and name it as “index.php”. This table will show the data in our database and the add form will add rows to our database. To create the table and form, open your HTML code editor and paste the code below after the tag.

      

?>
Firstname Lastname
">Edit ">Delete

Creating a Delete Script

Lastly, we create our delete script and name it as “delete.php”. This script will delete the row selected by our user. To create the script, open your HTML code editor and paste the code below after the tag.

Источник

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.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 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.

Источник

Оцените статью