Posted inphp / Ajax / JavaScript / jQuery / Mysql / Mysqli / Programming / Technology

PHP MySQL CRUD Create, Insert, Update and Delete operations

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

Creating the Config File


Creating the Landing Page




    
    Dashboard
    
    
    
    
    


    
0){ echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; while($row = mysqli_fetch_array($result)){ echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo ""; echo "
#NamedescriptionpriceAction
" . $row['id'] . "" . $row['name'] . "" . $row['description'] . "" . $row['price'] . ""; echo ""; echo ""; echo ""; echo "
"; // Free result set mysqli_free_result($result); } else{ echo "

No 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
    
    


    

Please fill this form and submit to add product record to the database.

" method="post">
Cancel

Creating the Read Page





    
    View Record
    
    


    

Back

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
    
    


    

Please edit the input values and submit to update the record.

Cancel

Creating the Delete Page





    
    View Record
    
    


    
" method="post">
"/>

Are you sure you want to delete this record?


No

Creating the Error Page




    
    Error
    
    


    

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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

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

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype