Write an article or Share a link

PHP MySQL CRUD Application (Build Blog Admin Panel Part-1)

5 years ago
PHP MySQL CRUD Application (Build Blog Admin Panel Part-1) by html hints

PHP MySQL CRUD Application (Build Blog Admin Panel Part-1)


In this Tutorial, Learn How To Buid Admin Panel Using PHP & MYSQL With CRUD Operation. CRUD Operation means that you will be able to Create, Read, Update & Delete. CRUD are basic data manipulation for database.

In this tutorial we'll create a simple PHP application know as Blog Admin Panel which will able to perform all these operations on a MySQL database table at one place.

SOURCE CODE DOWNLOAD


For Demo & Free Source Code Scroll down.

Quick Start, you need these files to start work on this:

  • 1. Create a database and import the blog.sql file
  • 2. Change the database settings in config.php to your own
  • 3. Create the Index/Main Page
  • 4. Creating the Create Page
  • 5. Creating the Read Page
  • 6. Creating the Update Page
  • 7. Creating the Delete Page
  • 8. Creating the Error Page
  • 9. Run index.php

STEP 1
THE DATABASE


After downloading source code file for this. You will find blog.sql file. That file, contains all information about database to be needed for this.

        
    
CREATE TABLE `blog` (
    `id` int(11) NOT NULL,
    `head` varchar(100) NOT NULL,
    `content` text NOT NULL,
    `tags` text NOT NULL,
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    

STEP 2
Create Config File


After creating Database, You need to configure your config file in-order to connectivity between your PHP code with MYSQL Database

After creating config file you need to add this file into other pages using the PHP require_once() function.

        
    
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$DB_SERVER = "localhost";
$DB_USERNAME = "root";
$DB_PASSWORD = "";
$DB_NAME = "demo";
    
/* Attempt to connect to MySQL database */
$link = new mysqli($DB_SERVER, $DB_USERNAME, $DB_PASSWORD, $DB_NAME);

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
    

STEP 3
Create Index Page


In Index Page, You will see all data which is stored in MYSQL Database with all CRUD Operations options. Also, There will be a option of Add New Blog on the top of the page which will take you to create page where you add data into database with the help of PHP.

#When No Record Found #When Record Found

In Action Area, you will see 3 Icons which are defined as Read, Update & Create. When you click on appropriate icons that action will happen with that appropriate data

STEP 4
Creating Create Page


In this page, You will have a simple form which will have 3 fields such as Head, Content & Tags. When you will fill this form & click on submit button all the data will be stored using PHP.

Below is the PHP Structure which will manipulate form values to insert that value into database

        
    
require_once "config.php";

// Define variables and initialize with empty values
$head = $content = $tags = "";
$head_err = $content_err = $tags_err = "";
    
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
    // Validate head
    $input_head = trim($_POST["head"]);
    if(empty($input_head)){
        $head_err = "Please enter a head.";
    } else{
        $head = $input_head;
    }
    
    // Validate content
    $input_content = trim($_POST["content"]);
    if(empty($input_content)){
        $content_err = "Please enter an content.";     
    } else{
        $content = $input_content;
    }
    
    // Validate tags
    $input_tags = trim($_POST["tags"]);
    if(empty($input_tags)){
        $tags_err = "Please enter the tags amount.";     
    } else{
        $tags = $input_tags;
    }
    
    // Check input errors before inserting in database
    if(empty($head_err) && empty($content_err) && empty($tags_err)){
        // Prepare an insert statement
        $sql = "INSERT INTO employees (head, content, tags) VALUES (?, ?, ?)";
            
        if($stmt = mysqli_prepare($link, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "sss", $param_head, $param_content, $param_tags);
            
            // Set parameters
            $param_head = $head;
            $param_content = $content;
            $param_tags = $tags;
            
            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                // Records created successfully. Redirect to landing page
                header("location: index.php");
                exit();
            } else{
                echo "Something went wrong. Please try again later.";
            }
        }
            
        // Close statement
        mysqli_stmt_close($stmt);
    }
    
    // Close connection
    mysqli_close($link);
}
    
#Output When You Run Create Page

STEP 5
Creating Read,Update & Delete Page


When you will successfully run create page you will able to insert data through that page using MYSQL Database & also you will see preview through index page.

Now, Its time to create other operations such as Read, Update & Delete. As same you done PHP code in Create Page to insert data into database as some same you need to do for Read, Update & Delete. Its all about SQL Query you are using to perform all operations

#Query For Read Data
        
    
$sql = "SELECT * FROM employees WHERE id = ?";
    
#Query For Update Data
        
    
$sql = "UPDATE employees SET head=?, content=?, tags=? WHERE id=?";
    
#Query For Delete Data
        
    
$sql = "DELETE FROM employees WHERE id = ?";
    

These queries are used to perform all CRUD Operation in PHP for better Understanding go-through Demo after that Download Below Files & perform testing on that & build your own PHP & MYSQL Operation

#This is Part-1, Part-2 Comming Soon

In PHP MySQL CRUD Application (Build Blog Admin Panel Part-2), You will learn how to make Admin Panel Login Protected & How to keep your data secure from unauthorized access.

For working example, You will get all files, when you download the source code. And after than you can edit it according to you

if you face any issues you can contact by asking question on Developers Community

PHPResources

We use cookies to ensure better User Experience. Read More