crud operations in php is an acronym for Select, Insert, Update, and Delete. CRUD operations are simply data manipulation for MySQL database. I have a already learned how to perform create (i.e. insert data), read (i.e. select data), edit with update as wel as soft and hard delete operations in previous best Articles. In this best post I will make a easy to use PHP based web application to perform all these insert, update and delete operations on a MySQL database table at single platform.
Good Luck, let’s start by making the Database table which I will use in all of our best example step by step.
Creating the Database Table
Execute the following SQL query to create a table named products inside your MySQL database. We will use this table for all of our future operations.
CREATE TABLE products ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100) NOT NULL, description VARCHAR(255) NOT NULL, price INT(10) NOT NULL );
PHP MySQL CRUD Application
crud operations in php
- PHP Jquery Ajax CRUD Example Tutorial From Scratch
- Reusable PHP CRUD DATABASE FUNCTIONS
- PHP OOPS – MySQL connecting Insert update delete select Fetch Data
Creating the Config File
Creating the Landing Page
Dashboard 0){ echo "products Details
Add New productNo records were found.
"; } } else{ echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); } // Close connection mysqli_close($link); ?>
Creating the Create Page
array("regexp"=>"/^[a-zA-Z\s]+$/")))){ $name_err = "Please enter a valid name."; } else{ $name = $input_name; } $input_description = trim($_POST["description"]); if(empty($input_description)){ $description_err = "Please enter an description."; } else{ $description = $input_description; } $input_price = trim($_POST["price"]); if(empty($input_price)){ $price_err = "Please enter the price amount."; } elseif(!ctype_digit($input_price)){ $price_err = "Please enter a positive integer value."; } else{ $price = $input_price; } if(empty($name_err) && empty($description_err) && empty($price_err)){ $sql = "INSERT INTO products (name, description, price) VALUES (?, ?, ?)"; if($stmt = mysqli_prepare($link, $sql)){ mysqli_stmt_bind_param($stmt, "sss", $param_name, $param_description, $param_price); $param_name = $name; $param_description = $description; $param_price = $price; if(mysqli_stmt_execute($stmt)){ header("location: index.php"); exit(); } else{ echo "Something went wrong. Please try again later."; } } mysqli_stmt_close($stmt); } mysqli_close($link); } ?>Create Record Create Record
Please fill this form and submit to add product record to the database.
Creating the Read Page
View Record
Creating the Update Page
array("regexp"=>"/^[a-zA-Z\s]+$/")))){ $name_err = "Please enter a valid name."; } else{ $name = $input_name; } $input_description = trim($_POST["description"]); if(empty($input_description)){ $description_err = "Please enter an description."; } else{ $description = $input_description; } $input_price = trim($_POST["price"]); if(empty($input_price)){ $price_err = "Please enter the price amount."; } elseif(!ctype_digit($input_price)){ $price_err = "Please enter a positive integer value."; } else{ $price = $input_price; } if(empty($name_err) && empty($description_err) && empty($price_err)){ $sql = "UPDATE products SET name=?, description=?, price=? WHERE id=?"; if($stmt = mysqli_prepare($link, $sql)){ mysqli_stmt_bind_param($stmt, "sssi", $param_name, $param_description, $param_price, $param_id); $param_name = $name; $param_description = $description; $param_price = $price; $param_id = $id; if(mysqli_stmt_execute($stmt)){ header("location: index.php"); exit(); } else{ echo "Something went wrong. Please try again later."; } } mysqli_stmt_close($stmt); } mysqli_close($link); } else{ if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){ $id = trim($_GET["id"]); $sql = "SELECT * FROM products WHERE id = ?"; if($stmt = mysqli_prepare($link, $sql)){ mysqli_stmt_bind_param($stmt, "i", $param_id); $param_id = $id; if(mysqli_stmt_execute($stmt)){ $result = mysqli_stmt_get_result($stmt); if(mysqli_num_rows($result) == 1){ $row = mysqli_fetch_array($result, MYSQLI_ASSOC); $name = $row["name"]; $description = $row["description"]; $price = $row["price"]; } else{ header("location: error.php"); exit(); } } else{ echo "Oops! Something went wrong. Please try again later."; } } mysqli_stmt_close($stmt); mysqli_close($link); } else{ header("location: error.php"); exit(); } } ?>Update Record Update Record
Please edit the input values and submit to update the record.
Creating the Delete Page
View Record Delete Record
Creating the Error Page
Error Invalid Request
Sorry, you've made an invalid request. Please go back and try again.
Summary
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about crud operations in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.