PHP CRUD OPERATIONS (Part 2)
We developed the base for our operations in the last article. In this session, we will be covering insertion and displaying of data.
Create Add Products page:
Create Add Products page:
To start you first need to put navigation to add button that lead you to add page.
Now create file with name add.php and then design input form to enter data.
Let’s understand this part of code in two section.
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 | <?php require 'main.php'; if ( !empty($_POST)) { // keep track validation errors $Pro_nameError = null; $Pro_descriptionError = null; $Pro_priceError = null; // keep track post values $Pro_name = $_POST['Pro_name']; $Pro_description = $_POST['Pro_description']; $Pro_price = $_POST['Pro_price']; // validate input $valid = true; if (empty($Pro_name)) { $Pro_nameError = 'Please enter Product Name'; $valid = false; } if (empty($Pro_description)) { $Pro_descriptionError = 'Please enter Pro_description'; $valid = false; } if (empty($Pro_price)) { $Pro_priceError = 'Please enter Pro_price'; $valid = false; } // insert data if ($valid) { $pdo = Products::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO products (Pro_name,Pro_description,Pro_price) values( ?, ?,?)"; $q = $pdo->prepare($sql); $q->execute(array($Pro_name,$Pro_description,$Pro_price)); Products::disconnect(); header("Location: index.php"); } } ?> |
In first section you check whether the form is submitted or not by checking $_POST variable. If so, now you have to check each entry to ensure they are not empty.
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 51 52 53 54 55 | <!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"> <script src="js/bootstrap.min.js"></script> <link href="css/style.css" rel="stylesheet" /> </head> <body> <div class="container"> <div class="span10 offset1"> <div class="row text-center"> <h3>Add New Product</h3> </div> <form class="form-horizontal" action="add.php" method="post"> <div class="control-group <?php echo !empty($Pro_nameError)?'error':'';?>"> <label class="control-label">NAME</label> <div class="controls"> <input name="Pro_name" type="text" placeholder="Name" value="<?php echo !empty($Pro_name)?$Pro_name:'';?>"> <?php if (!empty($Pro_nameError)): ?> <span class="help-inline"><?php echo $Pro_nameError;?></span> <?php endif;?> </div> </div> <div class="control-group <?php echo !empty($Pro_descriptionError)?'error':'';?>"> <label class="control-label">Description</label> <div class="controls"> <input name="Pro_description" type="text" placeholder="Description" value="<?php echo !empty($Pro_description)?$Pro_description:'';?>"> <?php if (!empty($Pro_descriptionError)): ?> <span class="help-inline"><?php echo $Pro_descriptionError;?></span> <?php endif;?> </div> </div> <div class="control-group <?php echo !empty($Pro_priceError)?'error':'';?>"> <label class="control-label">Price</label> <div class="controls"> <input name="Pro_price" type="text" placeholder="Price" value="<?php echo !empty($Pro_price)?$Pro_price:'';?>"> <?php if (!empty($Pro_priceError)): ?> <span class="help-inline"><?php echo $Pro_priceError;?></span> <?php endif;?> </div> </div> <div class="form-actions"> <button type="submit" class="btn"><i class="fa fa-check"></i>Add New</button> <a class="btn" href="index.php"><i class="fa fa-arrow-left"></i>Back</a> </div> </form> </div> </div> <!-- /container --> </body> </html> |
Second section is a simple HTML. You just design an input form.
1 | $sql = "INSERT INTO products (Pro_name,Pro_description,Pro_price) values( ?, ?,?)"; |
Note: You don’t need to enter “id” as auto increment is enabled and it will be automatically entered. Both the section will be in same file.
Create View page:
Here $_GET['id'] variable is used. That will try to find the “id” if it is not found, it redirects to "index.php" page. Otherwise, it will read data from database using the "id" field and store data into a PHP variable $data.
Query for viewing data:
1 | $sql="SELECT * FROM products where Pro_id = ?"; |
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | <?php require 'main.php'; $Pro_id = null; if ( !empty($_GET['Pro_id'])) { $Pro_id = $_REQUEST['Pro_id']; } if ( null==$Pro_id ) { header("Location: index.php"); } else { $pdo = Products::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "SELECT * FROM products where Pro_id = ?"; $q = $pdo->prepare($sql); $q->execute(array($Pro_id)); $data = $q->fetch(PDO::FETCH_ASSOC); Products::disconnect(); } ?> <!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"> <script src="js/bootstrap.min.js"></script> <link href="css/style.css" rel="stylesheet" /> </head> <body> <div class="container"> <div class="span10 offset1"> <div class="row"> <h3>Products</h3> </div> <table class="table table-striped table-bordered"> <thead> <tr> <th>Id</th> <th>Name</th> <th>Description</th> <th>Price</th> </tr> </thead> <tbody> <?php echo '<tr>';?> <?php echo '<td>'. $data['Pro_id'] . '</td>';?> <?php echo '<td>'. $data['Pro_name'] . '</td>';?> <?php echo '<td>'. $data['Pro_description'] . '</td>';?> <?php echo '<td>'. $data['Pro_price'] . '</td>';?> <?php echo '</tr>';?> </tbody> </table> <div class="form-actions"> <a class="btn" href="index.php"><i class="fa fa-arrow-left"></i>Back</a> </div> </div> </div> <!-- /container --> </body> </html> |
That's all from this session. We have insert and display the record from the database. There will be one more session, which will cover the updation and deletion of data.
0 comments