Wednesday 21 February 2018

CRUD Operation by MySqli procedural way in PHP


CRUD-Operation-by-MySqli-procedural-way-in-PHP
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a wide used open source general purpose server side scripting language and a very popular tool to create dynamic and interactive websites. PHP is a very popular and it can be embedded into HTML very easily. The best part of PHP is that it is widely used free and a very good alternative to other popular competitors like Microsoft ASP.Net. The PHP code is enclosed in special start and end tags like respectively that allows you to jump into and out of “PHP mode”. PHP can be downloaded for free from here. For a new comer PHP is very easy to learn but offers different advanced features for a professional programmer.

CRUD is an acronym for create, read, update and delete. In this lesson we will learn how to create a CRUD in PHP and MYSQLI and also how to search a record from a database. We will also learn a bit about HTML and CSS as well. If you would like to learn CRUD in visual basic.net using store procedure, then you can read that from CRUD in visual basic.net.
In this CRUD in PHP and MYSQLI we will see how to:
  1. Connect to Database
  2. Add new data to Database
  3. Select data from Database
  4. Edit record by id
  5. Delete record from the database
  6. Search records from the database
index.php
 <?php  
 include('conn.php');  
 ?>  
 <!DOCTYPE html>  
 <html lang="en">  
 <head>  
      <meta charset="UTF-8">  
      <title>CRUD using PHP &amp; MYSQL</title>  
      <style>  
           #wrapper{  
                width:960px;  
                margin:0 auto;  
                background-color: #e3e3e3;  
                text-align: center;  
           }  
           #search{  
                background-color: orange;  
                height: 40px;  
           }  
           #search input[type="text"]{  
                height: 25px;  
                width:400px;  
                margin-top: 5px;  
                outline: none;  
           }  
           #search input[type="submit"]{  
                background-color: #333;  
                color:#fff;  
                height: 32px;  
                width:100px;  
                margin-top: 5px;  
           }  
           table{  
                text-align: center;  
           }  
           table th{  
                background-color: #900;  
                color: #fff;  
                padding:0px 54px;  
                height: 30px;  
           }  
           table tr{  
                text-align: center;  
           }  
      </style>  
 </head>  
 <body>  
      <div id="wrapper">  
           <h1>CRUD using PHP &amp; MYSQL</h1>  
           <form action="index.php" method="POST">  
                <p><label for="name">Please enter your name:</label></p>  
                <p><input type="text" name="name"></p>  
                <p><label for="address">Please enter your Address:</label></p>  
                <p><input type="text" name="address"></p>  
                <p><label for="email">Please enter your email</label></p>  
                <p><input type="email" name="email"></p>  
                <p><input type="submit" name="insert" value="Save Record"></p>  
           </form>  
 <?php  
      if(isset($_POST['insert'])){  
           $name=$_POST['name'];  
           $address=$_POST['address'];  
           $email=$_POST['email'];  
           $query="insert into students (st_name,address,email) values ('$name','$address','$email')";  
            $row=mysqli_query($con,$query);  
           if($row){  
                echo'<script>alert("Record inserted successfully")</script>';  
           }  
      }  
 ?>  
           <div id="search">  
                <form action="search.php" method="GET">  
           <input type="text" name="search" placeholder=" search...">  
           <input type="submit" value="search">  
           </form>  
           </div>  
           <div id="records">  
                <h2>Records in the database table</h2>  
                <table>  
                     <tr>  
                          <th>ID</th>  
                          <th>NAME</th>  
                          <th>ADDRESS</th>  
                          <th>EMAIL</th>  
                          <th>EDIT</th>  
                          <th>DELETE</th>  
                     </tr>  
                     <?php  
                     $query="select * from students";  
                     $result=mysqli_query($con,$query);  
                     while ($row=mysqli_fetch_assoc($result)) {  
                               $id=$row['ID'];  
                               $name=$row['st_name'];  
                               $address=$row['address'];  
                               $email=$row['email'];  
                     ?>  
                     <tr>  
                               <td><?php echo $id;?></td>  
                               <td><?php echo $name;?></td>  
                               <td><?php echo $address;?></td>  
                               <td><?php echo $email;?></td>  
                               <td><a href="edit.php?edit=<?php echo $id ;?>">Edit</a></td>  
                               <td><a href="delete.php?del=<?php echo $id ;?>">Delete</a></td>  
                     </tr>  
 <?php  }?>  
                </table>  
           </div>  
      </div>  
 </body>  
 </html>  
conn.php
 <?php  
      $con=mysqli_connect('localhost','root','','youtube');  
 ?>  
edit.php
 <?php  
 include('conn.php');  
 ?>  
 <!DOCTYPE html>  
 <html lang="en">  
 <head>  
      <meta charset="UTF-8">  
      <title>CRUD using PHP &amp; MYSQL</title>  
      <style>  
           #wrapper{  
                width:960px;  
                margin:0 auto;  
                background-color: #e3e3e3;  
                text-align: center;  
           }  
           #search{  
                background-color: orange;  
                height: 40px;  
           }  
           #search input[type="text"]{  
                height: 25px;  
                width:400px;  
                margin-top: 5px;  
                outline: none;  
           }  
           #search input[type="submit"]{  
                background-color: #333;  
                color:#fff;  
                height: 32px;  
                width:100px;  
                margin-top: 5px;  
           }  
           table{  
                text-align: center;  
           }  
           table th{  
                background-color: #900;  
                color: #fff;  
                padding:0px 54px;  
                height: 30px;  
           }  
           table tr{  
                text-align: center;  
           }  
      </style>  
 </head>  
 <body>  
      <div id="wrapper">  
           <h1>CRUD using PHP &amp; MYSQL</h1>  
           <?php  
                $edit_id=$_GET['edit'];       
                $query="select * from students where id='$edit_id'";  
                $run=mysqli_query($con,$query);  
                while ($row=mysqli_fetch_assoc($run)) {  
                     $id=$row['ID'];  
                     $name=$row['st_name'];  
                     $address=$row['address'];  
                     $email=$row['email'];  
           ?>  
           <form action="edit.php?edit_form=<?php echo $id;?>" method="POST">  
                <p><label for="name">Please enter your name:</label></p>  
                <p><input type="text" name="name" value=<?php echo $name;?>></p>  
                <p><label for="address">Please enter your Address:</label></p>  
                <p><input type="text" name="address" value=<?php echo $address;?>></p>  
                <p><label for="email">Please enter your email</label></p>  
                <p><input type="email" name="email" value=<?php echo $email;?>></p>  
                <p><input type="submit" name="update" value="Update Record"></p>  
           </form>  
           <?php }?>  
      </div>  
 </body>  
 </html>  
 <?php  
      if(isset($_POST['update'])){  
           $update_id=$_GET['edit_form'];  
                $name=$_POST['name'];  
                 $address=$_POST['address'];  
                 $email=$_POST['email'];  
                $query="update students set st_name='$name',address='$address',email='$email' where id='$update_id'";  
                $row=mysqli_query($con,$query);  
                if($row){  
                     echo '<script>alert("Updated successfully")</script>';  
                     header('Location:index.php');  
                }  
           }  
 ?>  
delete.php
 <?php  
 include('conn.php');  
 ?>  
 <!DOCTYPE html>  
 <html lang="en">  
 <head>  
      <meta charset="UTF-8">  
      <title>CRUD using PHP &amp; MYSQL</title>  
      <style>  
           #wrapper{  
                width:960px;  
                margin:0 auto;  
                background-color: #e3e3e3;  
                text-align: center;  
           }  
           #search{  
                background-color: orange;  
                height: 40px;  
           }  
           #search input[type="text"]{  
                height: 25px;  
                width:400px;  
                margin-top: 5px;  
                outline: none;  
           }  
           #search input[type="submit"]{  
                background-color: #333;  
                color:#fff;  
                height: 32px;  
                width:100px;  
                margin-top: 5px;  
           }  
           table{  
                text-align: center;  
           }  
           table th{  
                background-color: #900;  
                color: #fff;  
                padding:0px 54px;  
                height: 30px;  
           }  
           table tr{  
                text-align: center;  
           }  
      </style>  
 </head>  
 <body>  
      <div id="wrapper">  
           <h1>CRUD using PHP &amp; MYSQL</h1>  
           <?php  
                $del_id=$_GET['del'];       
                $query="delete from students where id='$del_id'";  
                if(mysqli_query($con,$query)){  
                     header('LOCATION:index.php');  
                }  
           ?>  
           <form action="edit.php?edit_form=<?php echo $id;?>" method="POST">  
                <p><label for="name">Please enter your name:</label></p>  
                <p><input type="text" name="name" value=<?php echo $name;?>></p>  
                <p><label for="address">Please enter your Address:</label></p>  
                <p><input type="text" name="address" value=<?php echo $address;?>></p>  
                <p><label for="email">Please enter your email</label></p>  
                <p><input type="email" name="email" value=<?php echo $email;?>></p>  
                <p><input type="submit" name="update" value="Update Record"></p>  
           </form>  
      </div>  
 </body>  
 </html>  
search.php
 <?php  
 include('conn.php');  
 ?>  
 <!DOCTYPE html>  
 <html lang="en">  
 <head>  
      <meta charset="UTF-8">  
      <title>CRUD using PHP &amp; MYSQL</title>  
      <style>  
           #wrapper{  
                width:960px;  
                margin:0 auto;  
                background-color: #e3e3e3;  
                text-align: center;  
           }  
           #search{  
                background-color: orange;  
                height: 40px;  
           }  
           #search input[type="text"]{  
                height: 25px;  
                width:400px;  
                margin-top: 5px;  
                outline: none;  
           }  
           #search input[type="submit"]{  
                background-color: #333;  
                color:#fff;  
                height: 32px;  
                width:100px;  
                margin-top: 5px;  
           }  
           table{  
                text-align: center;  
           }  
           table th{  
                background-color: #900;  
                color: #fff;  
                padding:0px 94px;  
                height: 30px;  
           }  
           table tr{  
                text-align: center;  
           }  
      </style>  
 </head>  
 <body>  
      <div id="wrapper">  
           <h1>CRUD using PHP &amp; MYSQL</h1>  
           <div id="records">  
                <h2>Records in the database table</h2>  
                <table>  
                     <tr>  
                          <th>ID</th>  
                          <th>NAME</th>  
                          <th>ADDRESS</th>  
                          <th>EMAIL</th>  
                          </tr>  
                     <?php  
                if(isset($_GET['search'])){  
                     $search_name=$_GET['search'];  
                $query="select * from students where st_name LIKE '$search_name%'";  
                $result=mysqli_query($con,$query);  
                while ($row=mysqli_fetch_assoc($result)) {  
                          $id=$row['ID'];  
                           $name=$row['st_name'];  
                           $address=$row['address'];  
                           $email=$row['email'];  
                          ?>  
                     <tr>  
                               <td><?php echo $id;?></td>  
                               <td><?php echo $name;?></td>  
                               <td><?php echo $address;?></td>  
                               <td><?php echo $email;?></td>  
 <?php  }}?>  
                     </tr>  
                     <tr><td colspan="4"><a href="index.php">Home</a></td></tr>  
                </table>  
           </div>  
      </div>  
 </body>  
 </html>  
You can also check the video on my youtube channel.

MYSQL, PHP

0 comments:

Post a Comment

 

© 2018 Mastering Web Development: HTML, Bootstrap, PHP, ASP.NET & VB.NET Essentials - Designed by Mukund | Privacy Policy | Sitemap

About Me | Contact Me | Write For Us