Dear Reader, let us we know that “How to Create CURD Operation in PHP with MySQL and Bootstrap“. So in this Tutorial, We Learn Step By Step How to Create CURD Operation in PHP with MySQL and Bootstrap Process.
Step 1:- At first create database like ‘php_test‘
Step 2:- Create a table in “php_test” database like “products” table using below SQL code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
-- -- Table structure for table `products` -- CREATE TABLE IF NOT EXISTS `products` ( `id` int(11) NOT NULL, `product` varchar(60) NOT NULL, `category` int(6) NOT NULL, `sub_cat` int(6) NOT NULL, `price` double(16,2) NOT NULL, `discount` double(16,2) NOT NULL, `quantity` double(16,2) NOT NULL, `total_amount` double(16,2) NOT NULL, `status` int(1) NOT NULL, `type` varchar(50) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; |
Step 3:- Now we need to create a db.php file for configuring a database connection in our project and paste below code
1 2 3 4 5 6 7 8 9 10 11 |
$servername = "localhost"; $username = "root"; $password = ""; $database = "php_test"; // Create connection $con = new mysqli($servername, $username, $password,$database); // Check connection if ($con->connect_error) { die("Connection failed: " . $con->connect_error); } |
Step 4:- Now we need to create index.php file in our project and paste below code
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
<?php include('db.php'); if(isset($_REQUEST['id'])) { //echo 'welcome'; $sql_delete = "DELETE FROM products WHERE id = ".$_REQUEST['id']." "; $result_delete = mysqli_query($con,$sql_delete); if($result_delete == 1) { echo '<div class = "alert alert-success">Product deleted successfully</div>'; header("Location:index.php?status=delete"); } else { echo '<div class = "alert alert-danger">Product not deleted successfully</div>'; } } if(isset($_REQUEST['status']) && $_REQUEST['status'] == 'delete') { echo '<div class = "alert alert-success">Product deleted successfully</div>'; } if(isset($_REQUEST['status']) && $_REQUEST['status'] == 'add') { echo '<div class = "alert alert-success">Product added successfully</div>'; } if(isset($_REQUEST['status']) && $_REQUEST['status'] == 'edit') { echo '<div class = "alert alert-success">Product updated successfully</div>'; } ?> <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>Products</h2> <div class="table-responsive"> <table class="table"> <thead> <tr> <th colspan="5"></th> <th class="btn btn-success"><a href="add.php">Add</a></th> </tr> <tr> <th>S.N</th> <th>Product</th> <th>Price</th> <th>Quantity</th> <th>Edit</th> <th>Delete</th> </tr> </thead> <tbody> <?php $i = 1; $sql = "SELECT * FROM products"; $result = mysqli_query($con,$sql); while($row = mysqli_fetch_array($result)) { ?> <tr> <td><?php echo $i; ?></td> <td><?php echo $row['product']; ?></td> <td><?php echo $row['price']; ?></td> <td><?php echo $row['quantity']; ?></td> <td><a href="edit.php?id=<?php echo $row['id']; ?>"><span class="glyphicon glyphicon-edit"></span></a></td> <td><a href="index.php?id=<?php echo $row['id']; ?>"<span class="glyphicon glyphicon-trash"></span></a></td> </tr> <?php $i++; } ?> </tbody> </table> </div> </div> </body> </html> |
Step 5:- Now we need to create an add.php file in our project and paste below code
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
<?php include('db.php'); if(isset($_REQUEST['submit'])) { //echo 'welcome'; $product = $_REQUEST['product']; $cat = $_REQUEST['cat']; $sub_cat = $_REQUEST['sub_cat']; $price = $_REQUEST['price']; $quantity = $_REQUEST['quantity']; $discount = $_REQUEST['discount']; $total_amount = $_REQUEST['total_amount']; $status = $_REQUEST['status']; $type = $_REQUEST['type']; $product_type = ''; foreach ($type as $typeval) { $product_type .= $typeval.","; } $product_type = rtrim($product_type,','); $sql = "INSERT INTO products (product,category,sub_cat,price,quantity,discount,total_amount,status,type) VALUES('$product','$cat','$sub_cat','$price','$quantity','$discount','$total_amount','$status','$product_type')"; $result = mysqli_query($con,$sql); if($result == 1) { echo 'Product added'; header("Location:index.php?status=add"); } else { echo 'not added'.$conn->connect_error; } } ?> <!DOCTYPE html> <html lang="en"> <head> <title></title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>Product</h2> <form action="add.php" method="POST" name="add"> <div class="form-group"> <label for="email">Product:</label> <input type="text" class="form-control" id="product" placeholder="Enter product" name="product"> </div> <div class="form-group"> <label for="Category">Category:</label> <select type="select" name="cat" id="cat" class="form-control" onchange="get_sub_cat(this)"> <option value="0">Select</option> <option value="1">Mobile</option> <option value="2">Laptop</option> </select> </div> <div class="form-group"> <label for="email">Sub Category:</label> <select type="select" name="sub_cat" id="sub_cat" class="form-control"> <option value="0">Select</option> <option value="1">Samsung Mobile</option> <option value="2">Redmi Mobile</option> <option value="3">Dell Laptop</option> <option value="4">Apple Laptop</option> </select> </div> <div class="form-group"> <label for="Price">Price:</label> <input type="text" class="form-control" id="price" placeholder="Enter price" name="price" onkeyup="calculate()"> </div> <div class="form-group"> <label for="Quantity">Quantity:</label> <input type="text" class="form-control" id="quantity" placeholder="Enter quantity" name="quantity" onkeyup="calculate()"> </div> <div class="form-group"> <label for="Discount">Discount:</label> <input type="text" class="form-control" id="discount" placeholder="Enter discount" name="discount" onkeyup="calculate()"> </div> <div class="form-group"> <label for="Total Amount">Total Amount:</label> <input type="text" class="form-control" id="total_amount" placeholder="Total Amount" name="total_amount" readonly> </div> <div class="checkbox"> <label><input type="checkbox" name="type[]" value="1"> Old</label> <label><input type="checkbox" name="type[]" value="2"> Latest</label> </div> <div class="checkbox"> <label><input type="radio" name="status" value="1" checked="checked"> Active</label> <label><input type="radio" name="status" value="2"> Inactive</label> </div> <button type="submit" name = "submit" class="btn btn-default">Submit</button> </form> </div> </body> </html> <script type="text/javascript"> /*----------- for calculate total amount start -----------*/ function calculate() { //alert('hii'); var price = $('#price').val(); if(price == '') { price = 0; } var discount = $('#discount').val(); if(discount == '') { discount = 0; } var quantity = $('#quantity').val(); if(quantity == '') { quantity = 1; } var net_price = parseFloat(price) * parseFloat(quantity); //alert(net_price); var total_amount = net_price - discount; $('#total_amount').val(total_amount); } /*----------- for calculate total amount close -----------*/ </script> |
Step 6:- Now we need to create an edit.php file in our project and paste below code
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
<?php include('db.php'); if(isset($_REQUEST['submit'])) { //echo 'welcome'; $product = $_REQUEST['product']; $cat = $_REQUEST['cat']; $sub_cat = $_REQUEST['sub_cat']; $price = $_REQUEST['price']; $quantity = $_REQUEST['quantity']; $discount = $_REQUEST['discount']; $total_amount = $_REQUEST['total_amount']; $status = $_REQUEST['status']; $type = $_REQUEST['type']; $product_type = ''; foreach ($type as $typeval) { $product_type .= $typeval.","; } $product_type = rtrim($product_type,','); $sql = "UPDATE products SET product = '$product',category='$cat',sub_cat='$sub_cat',price='$price',quantity='$quantity',discount='$discount',total_amount='$total_amount',status='$status',type='$product_type' WHERE id = ".$_REQUEST['id']." "; $result = mysqli_query($con,$sql); if($result == 1) { echo 'Product updated'; header("Location:index.php?status=edit"); } else { echo 'not updated'.$conn->connect_error; } } $sql = "SELECT * FROM products WHERE id = ".$_REQUEST['id']." "; $result = mysqli_query($con,$sql); $row = mysqli_fetch_array($result); ?> <!DOCTYPE html> <html lang="en"> <head> <title></title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>Product</h2> <form action="edit.php?id=<?php echo $row['id']; ?>" method="POST" name="add"> <div class="form-group"> <label for="email">Product:</label> <input type="text" class="form-control" id="product" placeholder="Enter product" name="product" value="<?php echo $row['product']; ?>"> </div> <div class="form-group"> <label for="Category">Category:</label> <select type="select" name="cat" id="cat" class="form-control"> <option value="0">Select</option> <option value="1" <?php if($row['category'] == 1){ echo 'selected'; } ?>>Mobile</option> <option value="2" <?php if($row['category'] == 2){ echo 'selected'; } ?>>Laptop</option> </select> </div> <div class="form-group"> <label for="email">Sub Category:</label> <select type="select" name="sub_cat" id="sub_cat" class="form-control"> <option value="0">Select</option> <option value="1" <?php if($row['sub_cat'] == 1){ echo 'selected'; } ?>>Samsung Mobile</option> <option value="2" <?php if($row['sub_cat'] == 2){ echo 'selected'; } ?>>Redmi Mobile</option> <option value="3" <?php if($row['sub_cat'] == 3){ echo 'selected'; } ?>>Dell Laptop</option> <option value="4" <?php if($row['sub_cat'] == 4){ echo 'selected'; } ?>>Apple Laptop</option> </select> </div> <div class="form-group"> <label for="Price">Price:</label> <input type="text" class="form-control" id="price" placeholder="Enter price" name="price" value="<?php echo $row['price']; ?>" onkeyup="calculate()"> </div> <div class="form-group"> <label for="Quantity">Quantity:</label> <input type="text" class="form-control" id="quantity" placeholder="Enter quantity" name="quantity" value="<?php echo $row['quantity']; ?>" onkeyup="calculate()"> </div> <div class="form-group"> <label for="Discount">Discount:</label> <input type="text" class="form-control" id="discount" placeholder="Enter discount" name="discount" value="<?php echo $row['discount']; ?>" onkeyup="calculate()"> </div> <div class="form-group"> <label for="Total Amount">Total Amount:</label> <input type="text" class="form-control" id="total_amount" placeholder="Total Amount" name="total_amount" value="<?php echo $row['total_amount']; ?>" readonly> </div> <?php $type_arr = explode(",",$row['type']); //print_r($type_arr); ?> <div class="checkbox"> <label><input type="checkbox" name="type[]" value="1" <?php if(in_array(1, $type_arr)){ ?> checked="checked" <?php } ?>> Old</label> <label><input type="checkbox" name="type[]" value="2" <?php if(in_array(2, $type_arr)){ ?> checked="checked" <?php } ?>> Latest</label> </div> <div class="checkbox"> <label><input type="radio" name="status" value="1" <?php if($row['status'] == 1){ ?> checked="checked" <?php } ?>> Active</label> <label><input type="radio" name="status" value="2" <?php if($row['status'] == 2){ ?> checked="checked" <?php } ?>> Inactive</label> </div> <button type="submit" name = "submit" class="btn btn-default">Submit</button> </form> </div> </body> </html> <script type="text/javascript"> /*----------- for calculate total amount start -----------*/ function calculate() { //alert('hii'); var price = $('#price').val(); if(price == '') { price = 0; } var discount = $('#discount').val(); if(discount == '') { discount = 0; } var quantity = $('#quantity').val(); if(quantity == '') { quantity = 1; } var net_price = parseFloat(price) * parseFloat(quantity); //alert(net_price); var total_amount = net_price - discount; $('#total_amount').val(total_amount); } /*----------- for calculate total amount close -----------*/ </script> |
Step 6:- Now we need to run our project, open browser, and type in URL localhost/your_project_name/index.php
like
localhost/php_test/index.php
you can see the result as we can see in below snapshot
Congratulations you have successfully create CURD Operation in PHP using MySQL and Bootstrap, if you like this post and was helpful for you then share this post on social media and if you have any query then please contact us or comment below, Thanks.