PHP CRUD OPERATIONS (Part 1)

When you are working on PHP web development, CRUD is frequently executed. CRUD stands for create, read/view, update, delete.

This is done using several step that are described below:

Step1: Create database
To implement PHP current operations, essentially and primarily you have to install EasyPHP. In EasyPHP tables (Products) are manually created.


Step2: Create file named “main.php”:
After creating table, now you create file with the name "main.php" that mainly takes care of all the stuff related to database connection and disconnection. To use this class (main.php), you need to supply correct values for $dbName, $dbHost, $dbUsername, $dbUserPassword as stated in code below.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
class Products
{
    private static $dbName = 'products' ;
    private static $dbHost = 'localhost' ;
    private static $dbUsername = 'root';
    private static $dbUserPassword = '';
    private static $cont  = null;
    
    public function __construct() {
        die('Init function is not allowed');
    }
    
    public static function connect()
    {
       // One connection through whole application
       if ( null == self::$cont )
       {    
        try
        {
          self::$cont =  new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbUserPassword);
        }
        catch(PDOException $e)
        {
          die($e->getMessage());
        }
       }
       return self::$cont;
    }
    
    public static function disconnect()
    {
        self::$cont = null;
    }
}
?>


  • $dbName: Database name which you use to store 'products' table.
  • $dbHost: Database host, this is normally "localhost".
  • $dbUsername: Database username.
  • $dbUserPassword: Database user's password

      This class has three main functions:
  • construct(): This is the constructor of class ( Constructor allows appropriate initialization of newly created object) .
  •  connect: This is the main function of this class. We use Database::connect() to create a connection.
  • disconnect: Disconnect from database. It simply sets connection to NULL. 

Step3: Create file “index.php”:
In third step you create the grid that will contain the data and present to user


Right now you can only add data manually to tables as we did:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link   href="css/bootstrap.min.css" rel="stylesheet">
            <link rel="stylesheet" href="css/font-awesome.min.css">
            <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
            <link href="css/style.css" rel="stylesheet" />
    <script src="js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container">
            <div class="row ">
                <h3>PHP Current Tutorial</h3>
            </div>
            <div class="row">
                  
                <table class="table table-striped table-bordered">
                  <thead>
                    <tr>
                      <th>Id</th>
                      <th>Name</th>
                      <th>Description</th>
                     <th>Price</th>
                     <th>Action</th>
                    </tr>
                  </thead>
                  <tbody>
                  <?php
                   include 'main.php';
                   $pdo = Products::connect();
                   $sql = 'SELECT * FROM products Order BY Pro_id';
                   foreach ($pdo->query($sql) as $row) {
                            echo '<tr>';
                            echo '<td>'. $row['Pro_id'] . '</td>';
                            echo '<td>'. $row['Pro_name'] . '</td>';
                            echo '<td>'. $row['Pro_description'] . '</td>';
                          echo '<td>'. $row['Pro_price'] . '</td>';
                          echo '</td>';
                          echo '</tr>';
                   }
                   Products::disconnect();
                  ?>
                  </tbody>
            </table>
        </div>
    </div> <!-- /container -->
  </body>
</html>

Step 4: Add the current operations button.

  • Now put on a button that add products :

                Below is the code of adding button, put it after line 17

1
2
3
<p>
  <a href="add.php" class="btn btn-success"><i class="fa fa-plus-circle"></i> Add                                    Products</a>
</p>


  • Now add view, update, and delete button:



Add below mentioned code after line 42 to add Action buttons


1
2
3
4
5
6
7
echo '<td width=300>';
echo '<a class="btn" href="view.php?Pro_id='.$row['Pro_id'].'"><i class="fa fa-eye"></i> View</a>';
echo ' ';
echo '<a class="btn" href="update.php?Pro_id='.$row['Pro_id'].'"><i class="fa fa-pencil-square-o"></i>Update</a>';
echo ' ';
echo '<a class="btn" href="delete.php?Pro_id='.$row['Pro_id'].'"><i class="fa  fa-trash-o"></i>Delete</a>';
echo '</td>';

That's all from this session. We have covered the basics so far. Proper add, delete, update, read pages will be created in the upcoming sessions..

You Might Also Like

0 comments

About Us

Digitorum Solutions is a Web Development setup initiated a group of university students. We are specialised in HTML5, CSS3, BootStrap, JavaScript, PhP and Server Side Languages. This is a dedicated, hardworking and passionate team working together to provide reliable services in the above mentioned categories of Web Development.

Drop Your Message