PHP Script for CRUD Application

Date :06/12/2018

What is CRUD?

CRUD refers to the four basic types of Database operations: Create, Read, Update, Delete. Most applications and projects perform  CRUD functionality.

This is a important script for all PHP beginners. Once you learn about these CRUD operations, you can use them for many projects.

Executing a CRUD Script

Step by step procedure are as follows;

STEP 1:
  • In this script, I have taken a example as testimonial section for CRUD usage.
  • In a PHPMYADMIN  , create a database Name “testimonials
  • Create a table name as “users
  • Create a table structure of rows having  id , rating, clientname, reviews.  I have attached a snapshot below,

This image has an empty alt attribute; its file name is crud1-1024x291.png

STEP 2:

Create a connection.php file in your favorite text editor and execute the below script to make connection with your DB.

<?php

$databaseHost = 'localhost';
$databaseName = 'testimonials';
$databaseUsername = 'root';
$databasePassword = '';

$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName); 
 
?>
Step 3:

Create add.php file and execute the below script for adding the Information and to insert a queries in to DB.

The programming script is with Enhanced CSS quality. Take a look on it and make use of it,

<html>
<style>
input[type=text] {
    color: #777;
    padding-left: 10px;
    margin: 10px;
    margin-top: 12px;
    margin-left: 18px;
    width: 290px;
    height: 35px;
    border: 1px solid #c7d0d2;
    border-radius: 2px;
    box-shadow: inset 0 1.5px 3px rgba(190, 190, 190, .4), 0 0 0 5px #f5f7f8;
}

  
   input[type=submit] {
    float: left;
    margin-right: 20px;
 
    width: 100px;
    height: 30px;
    font-size: 14px;
    font-weight: bold;
    color: #fff;
    background-color: #acd6ef; /*IE fallback*/
    background-image: -webkit-gradient(linear, left top, left bottom, from(#acd6ef), to(#6ec2e8));
    background-image: -moz-linear-gradient(top left 90deg, #acd6ef 0%, #6ec2e8 100%);
    background-image: linear-gradient(top left 90deg, #acd6ef 0%, #6ec2e8 100%);
    border-radius: 30px;
    border: 1px solid #66add6;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .3), inset 0 1px 0 rgba(255, 255, 255, .5);
    cursor: pointer;
   }
</style>
<body>
  <a href="list.php"><input type="submit" value="<< Added " name="Go Back" /></a>
  <br/><br/>

  <form action="add.php" method="post" name="form1">
    <table width="25%" border="0">
      <tr> 
        <td><span style="color:blue">rating</span></td>
        <td><input type="text" name="rating"></td>
      </tr>
      <tr> 
        <td><span style="color:blue">clientname</span></td>
        <td><input type="text" name="clientname"></td>
      </tr>
      <tr> 
          <td><span style="color:blue">reviews</span></td>
        <td><input type="text" name="reviews"></td>
      </tr>
      <tr> 
        
        <td><input type="submit" name="Submit" value="Add >>"></td>
      </tr>
      
    </table>
  </form>
  
  <?php

  // Check If form submitted, insert form data into users table.
   if(isset($_POST['Submit'])) {
    $rating = $_POST['rating'];
    $clientname = $_POST['clientname'];
    $reviews = $_POST['reviews'];
    
    // include database connection file
    include_once("connection.php");
        
    // Insert user data into table
    $result = mysqli_query($mysqli, "INSERT INTO users(rating,clientname,reviews) VALUES('$rating','$clientname','$reviews')");
    
    
  }
  
     ?>
  
</body>
</html>


Once You execute this in XAMPP , the output will be like this.

STEP 4:

Create edit.php file and execute the below script .Once you edited, You will see changes in DB and you can modify the added contents.

<?php
// include database connection file
include_once("connection.php");

// Check if form is submitted for user update, then redirect to homepage after update
if(isset($_POST['update']))
{	
	
	$id = $_POST['id'];
	
	$rating=$_POST['rating'];
	$clientname=$_POST['clientname'];
	$reviews=$_POST['reviews'];
		
	// update user data
	$result = mysqli_query($mysqli, "UPDATE users SET rating='$rating',clientname='$clientname',reviews='$reviews' WHERE id=$id");
	
	// Redirect to homepage to display updated user in list
	header("Location: list.php");
}
?&gt;
<?php
// Display selected user data based on id
// Getting id from url
$id = $_GET['id'];

// Fetech user data based on id
$result = mysqli_query($mysqli, "SELECT * FROM users WHERE id=$id");

while($user_data = mysqli_fetch_array($result))
{
	$rating = $user_data['rating'];
	$clientname = $user_data['clientname'];
	$reviews = $user_data['reviews'];
	
}
?&gt;
<html&gt;
<head&gt;	
	<title&gt;Edit User Data</title&gt;
</head&gt;
 <style&gt;
    input[type=text] {
    color: #777;
    padding-left: 10px;
    margin: 10px;
    margin-top: 12px;
    margin-left: 18px;
    width: 290px;
    height: 35px;
    border: 1px solid #c7d0d2;
    border-radius: 2px;
    box-shadow: inset 0 1.5px 3px rgba(190, 190, 190, .4), 0 0 0 5px #f5f7f8;
}
  input[type=submit] {
    float: left;
    margin-right: 20px;
    width: 180px;
    height: 30px;
    font-size: 14px;
    font-weight: bold;
    color: #fff;
    background-color: #acd6ef; /*IE fallback*/
    background-image: -webkit-gradient(linear, left top, left bottom, from(#acd6ef), to(#6ec2e8));
    background-image: -moz-linear-gradient(top left 90deg, #acd6ef 0%, #6ec2e8 100%);
    background-image: linear-gradient(top left 90deg, #acd6ef 0%, #6ec2e8 100%);
    border-radius: 30px;
    border: 1px solid #66add6;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .3), inset 0 1px 0 rgba(255, 255, 255, .5);
    cursor: pointer;
   }
   </style&gt;
   <body&gt;
   
	<a href="list.php"&gt;<input type="submit" name="Submit" value="Home Listings"&gt;</a&gt;
	<br/&gt;<br/&gt;
	
	<form name="update_user" method="post" action="edit.php"&gt;
		<table border="0"&gt;
			<tr&gt; 
				<td&gt;<span style="color:blue"&gt;rating</span&gt;</td&gt;
				<td&gt;<input type="text" name="rating" value=<?php echo $rating;?&gt;&gt;</td&gt;
			</tr&gt;
			<tr&gt; 
				<td&gt;<span style="color:blue"&gt;clientname</span&gt;</td&gt;
				<td&gt;<input type="text" name="clientname" value=<?php echo $clientname;?&gt;&gt;</td&gt;
			</tr&gt;
			<tr&gt; 
				<td&gt;<span style="color:blue"&gt;reviews</span&gt;</td&gt;
				<td&gt;<input type="text" name="reviews" value=<?php echo $reviews;?&gt;&gt;</td&gt;
			</tr&gt;
			<tr&gt;
				<td&gt;<input type="hidden" name="id" value=<?php echo $_GET['id'];?&gt;&gt;</td&gt;
				<td&gt;<input type="submit" name="update" value="Update"&gt;</td&gt;
			</tr&gt;
		</table&gt;
	</form&gt;
</body&gt;
</html&gt;


STEP 5:

Create a file called  delete.php  , This will delete the content and directly delete in DB.

<?php
// include database connection file

include_once("connection.php");

// Get id from URL to delete that user
$id = $_GET['id'];

// Delete user row from table based on given id
$result = mysqli_query($mysqli, "DELETE FROM users WHERE id=$id");

// After delete redirect to Home, so that latest user list will be displayed.
header("Location:list.php");
?&gt;

The output will look like this,

You can Edit / Delete a content  Both in UI and DB

Step 6:

I named a file as “list.php” , this is index  page for a total crud.

<?php
// Create database connection using config file
include_once("connection.php");

// Fetch all users data from database
$result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id DESC");
?&gt;

<html&gt;
<head&gt;    
    <title&gt;Homepage</title&gt;
</head&gt;
<style&gt;
<style&gt;
#rating {
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

#rating td, #rating th {
    border: 1px solid #ddd;
    padding: 8px;
}

#rating tr:nth-child(even){background-color: #f2f2f2;}

#rating tr:hover {background-color: #ddd;}

#rating th {
    padding-top: 12px;
    padding-bottom: 12px;
    text-align: left;
    background-color: blue;
    color: white;
}

input[type=submit] {
    float: left;
    margin-right: 20px;
    width: 180px;
    height: 30px;
    font-size: 14px;
    font-weight: bold;
    color: #fff;
    background-color: #acd6ef; /*IE fallback*/
    background-image: -webkit-gradient(linear, left top, left bottom, from(#acd6ef), to(#6ec2e8));
    background-image: -moz-linear-gradient(top left 90deg, #acd6ef 0%, #6ec2e8 100%);
    background-image: linear-gradient(top left 90deg, #acd6ef 0%, #6ec2e8 100%);
    border-radius: 30px;
    border: 1px solid #66add6;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .3), inset 0 1px 0 rgba(255, 255, 255, .5);
    cursor: pointer;
   }
</style&gt;
</style&gt;

<body&gt;
<a href="add.php"&gt;<input type="submit" name="Submit" value="Add Ratings"&gt;</a&gt;<br/&gt;<br/&gt;

   
	<table id = "rating"&gt;

    <tr&gt;
        <th&gt;rating</th&gt; <th&gt;clientname</th&gt; <th&gt;reviews</th&gt; <th&gt;Update</th&gt;
    </tr&gt;
    <?php  
    while($user_data = mysqli_fetch_array($result)) {         
        echo "<tr&gt;";
        echo "<td&gt;".$user_data['rating']."</td&gt;";
        echo "<td&gt;".$user_data['clientname']."</td&gt;";
        echo "<td&gt;".$user_data['reviews']."</td&gt;";    
        echo "<td&gt;<a href='edit.php?id=$user_data[id]'&gt;Edit</a&gt; | <a href='delete.php?id=$user_data[id]'&gt;Delete</a&gt;</td&gt;</tr&gt;";        
    }
    ?&gt;
    </table&gt;
</body&gt;
</html&gt;

In the above example, the effective usage of  crud is explained clearly, you can modify the script according to your  application and make benefit of it. 

Leave a Reply