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

Читайте также:  Java parameter name to string

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 Update Delete in PHP on Same Page

To fetch insert update delete data in PHP MySQL database with source code. In this tutorial, we will show you how to fetch insert update delete data in PHP from MySQL database using jQuery ajax with source code.

In this example, we will use jQuery, ajax, MySQL, and Bootstrap with PHP to create insert update delete on same page or one-page app with source code.

Note that, using jQuery and ajax we will insert, edit and update data from MySQL database in PHP on same page app.

How to Insert Update Delete in PHP on Same Page

  • Step 1 – Create a Database
  • Step 2 – Connecting To Database using PHP
  • Step 3 – Retrieve All Data From Database and Display in HTML Table
  • Step 4 – Edit Data From Database
  • Step 5 – Insert and Update Data Into Database
  • Step 6 – Delete Data From Database

Step 1 – Create a Database

In step 1, Open your browser and navigate to your phpmyadmin. Then run the following query to create database and table:

CREATE DATABASE demo; CREATE TABLE `users` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(255) NULL DEFAULT NULL, `age` VARCHAR(255) NULL DEFAULT NULL, `email` VARCHAR(255) NULL DEFAULT NULL, `created` DATETIME NULL DEFAULT NULL, PRIMARY KEY (`id`) ) COLLATE='latin1_swedish_ci' ENGINE=InnoDB AUTO_INCREMENT=1; INSERT INTO `users` (`id`, `name`, `age`, `email`, `created`) VALUES (NULL, 'Tiago', '26', '[email protected]', NULL), (NULL, 'Anil', '28', '[email protected]', NULL);

Step 2 – Connecting To Database using PHP

Then create one file name db.php, which is used to connect app from database:

Then add the following code into it:

Step 3 – Retrieve All Data From Database and Display in HTML Table

Now, you need to create Index.php file and add the following code into it:

         

Insert Update Delete in PHP On Same Page

# Name Age Email Action num_rows > 0): ?> ">Edit ">Delete No Data Found

Note that, index.php file code will display all users list from database. And as well as insert and edit data from mysql database using bootstrap model and ajax.

Step 4 – Edit Data From Database

In this step, Create edit.php file to get single record data from mysql database. So, add the following code into edit.php:

Step 5 – Insert and Update Data Into Database

In this step, insert and add data from mysql database. So, Create insert-update.php file to insert and update record data from mysql database. So, add the following code into insert-update.php:

0) < include 'db.php'; $name = $_POST['name']; $age = $_POST['age']; $email = $_POST['email']; if(empty($_POST['id']))< $query = "INSERT INTO users (name,age,email) VALUES ('$name','$age','$email')"; >else < $query = "UPDATE users set . $_POST['id'] . "', name='" . $_POST['name'] . "', age='" . $_POST['age'] . "', email='" . $_POST['email'] . "' WHERE . $_POST['id'] . "'"; >$res = mysqli_query($dbCon, $query); if($res) < echo json_encode($res); >else < echo "Error: " . $sql . "" . mysqli_error($dbCon); >> ?>

Step 6 – Delete Data From Database

Final step, delete data from mysql database. So, create delete.php file, which is delete data from mysql database. Now you need to add the following code into delete.php file:

Conclusion

That’s it, In this tutorial, we have learned how to fetch insert update delete data in PHP from MySQL database using jQuery ajax.

Источник

Select Insert Update Delete Record using PHP and MySQL

How to insert update delete record using php and mysql. In this tutorial we will show you step by step how to insert update delete record using php and mysql.

As well as you can use this insert update delete in php with source code to personally and professinally.

How to Select Insert Update Delete Record using PHP and MySQL

  • DB.PHP – Connecting Database
  • Insert.PHP – Insert Records Into MySQL DB
  • Select.php – Select Record From MySQL DB
  • Update.php – Update Record Into MySQL DB
  • Delete.php – Delete Record From MySQL DB
  • In starting, we will create MySQL database connection file in PHP.
    • Will use mysqli_connect() function to connecting database with PHP.

    1 – DB.PHP – Connecting Database

    First of all, Create db.php file to connecting mysql database with PHP:

    2 – Insert.PHP – Insert Records Into MySQL DB

    Then, Create a new file named insert.php and add the following code into insert.php:

    Note that, The SQL INSERT statement will insert record in MySQL database using PHP function.

    3 – Select.php – Select Record From MySQL DB

    Then, create a new file named Select.php and add the following code into selete.php:

     while($data = mysqli_fetch_array($query)) < echo "Id = ".$data['id']."
    "; echo "Firstname = ".$data['firstname']."
    "; echo "Lastname = ".$data['lastname']."
    "; echo "Mobile = ".$data['mobile']."

    "; > ?>

    Note that, The SQL SELECT statement will SELECT record FROM MySQL database using PHP function.

    If you want to select limited records from MySQL database, you can use LIMIT clause with SQL query like following:

    $sql = "SELECT * FROM users Limit 10";

    Or, if you want to fetch record with ascending or descending order. So, you can use ASC OR DESC with SQL query.

    4 – Update.php – Update Record Into MySQL DB

    Now, you can modify database table records using SQL “UPDATE “ statement query.

    So, Create a new file named Update.php and add the following code into update.php:

    5 – Delete.php – Delete Record From MySQL DB

    If you want to delete some records or all records from database, you can use DELETE SQL statement with PHP.

    So, Create a new file named Delete.php and add the following code into delete.php:

    Источник

    Use of Insert Update and Delete in PHP

    Output

    Updata data in PHP

     else < die("Please try again"); >// Using UPDATE query $query = "UPDATE students SET name = '$name',address = '$address', " . "WHERE $result = mysqli_query($connection, $query); if (!$result) < die('query FAILED' . mysqli_error()); >> ?>       

    Student Form

    Delete the record from the database in PHP

     else < die("Please try again"); >$query = "DELETE FROM students WHERE $result = mysqli_query($connection, $query); if (!$result) < die('query FAILED' . mysqli_error()); >> ?>       

    Student Form

    Источник

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