Use of Insert Update and Delete in PHP

Create records and send data in database table

To insert a data into a database we use the Insert query 

add_student.php

<?php
   if (isset($_POST['submit'])){
   	$name = $_POST['name'];
   	$address = $_POST['address'];
   	$class = $_POST['class'];
   	$connection = mysqli_connect('localhost', 'root', '', 'school');
   	if ($connection){
   		echo "Your connection has been established";
   	}else{
   		die("Please try again");
   	}
   
   	// Using INSERT query
   
   	$query = "INSERT INTO students(name,address,class)";
   	$query.= "VALUES('$name','$address','$class')";
   	$result = mysqli_query($connection, $query);
   	if (!$result){
   		die('query FAILED' . mysqli_error());
   	}else{
           echo "Student Saved";
       }
   }
   ?>
<html>
   <head>
      <title>This is demo</title>
      <meta charset="utf-8">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
   </head>
   <body>
      <div class="container">
         <h2>Student Add Form</h2>
         <form class="form-horizontal" action="add_student.php" method="post">
            <div class="form-group">
               <label class="control-label col-sm-2">Name:</label>
               <div class="col-sm-7">
                  <input type="text" class="form-control" id="name" placeholder="Enter Name" name="name">
               </div>
            </div>
            <div class="form-group">
               <label class="control-label col-sm-2">Address:</label>
               <div class="col-sm-7">
                  <input type="text" class="form-control" id="address" placeholder="Enter Address" name="address">
               </div>
            </div>
            <div class="form-group">
               <label class="control-label col-sm-2">Class:</label>
               <div class="col-sm-7">
                  <input type="text" class="form-control" id="class" placeholder="Enter Class" name="class">
               </div>
            </div>
            <div class="form-group">
               <div class="col-sm-offset-2 col-sm-10">
                  <button type="submit" name="submit" class="btn btn-success">Update</button>
               </div>
            </div>
         </form>
      </div>
   </body>
</html>

 

Use of print_r function in PHP

The print_r() function is used to print the data .It is used to read  the information in the database with PHP.

students.php

<?php
   $connection = mysqli_connect('localhost', 'root', '', 'school');
   If ($connection) {
       echo "Your connection has been established";
   } else {
       die("Please try again");
   }
   
   $query  = "SELECT * FROM students";
   $result = mysqli_query($connection, $query);
   
   if (!$result) {
       die('query FAILED' . mysqli_error());
   }
   
   ?>
<html>
   <head>
      <title>This is demo</title>
      <meta charset="utf-8">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
   </head>
   <body>
      <div class="container">
         <h2>Student Form</h2>
         <?php
            while ($row = mysqli_fetch_assoc($result)) {
            print_r($row);
            }
            
            ?>
      </div>
   </body>
</html>

 Output

 

Updata data in PHP

student_update.php

<?php
   if (isset($_POST['submit'])) {
       $id      = $_POST['id'];
       $name    = $_POST['name'];
       $address = $_POST['address'];
       $class   = $_POST['class'];
       
       $connection = mysqli_connect('localhost', 'root', '', 'school');
       
       If ($connection) {
           echo "Your connection has been established";
       } else {
           die("Please try again");
       }
       
       // Using UPDATE query
       $query  = "UPDATE students SET name = '$name',address = '$address', class = '$class' " . "WHERE id = $id";
       $result = mysqli_query($connection, $query);
       if (!$result) {
           die('query FAILED' . mysqli_error());
       }
       
   }
   
   ?>
<html>
   <head>
      <title>This is demo</title>
      <meta charset="utf-8">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
   </head>
   <body>
      <div class="container">
         <h2>Student Form</h2>
         <form class="form-horizontal" action="student_update.php" method="post">
            <div class="form-group">
               <label class="control-label col-sm-2">Id:</label>
               <div class="col-sm-7">
                  <input type="text" class="form-control" id="Id" name="id">
               </div>
            </div>
            <div class="form-group">
               <label class="control-label col-sm-2">Name:</label>
               <div class="col-sm-7">
                  <input type="text" class="form-control" id="name" placeholder="Enter Name" name="name">
               </div>
            </div>
            <div class="form-group">
               <label class="control-label col-sm-2">Address:</label>
               <div class="col-sm-7">
                  <input type="text" class="form-control" id="address" placeholder="Enter Address" name="address">
               </div>
            </div>
            <div class="form-group">
               <label class="control-label col-sm-2">Class:</label>
               <div class="col-sm-7">
                  <input type="text" class="form-control" id="class" placeholder="Enter Class" name="class">
               </div>
            </div>
            <div class="form-group">
               <div class="col-sm-offset-2 col-sm-10">
                  <button type="submit" name="submit" class="btn btn-success">Update</button>
               </div>
            </div>
         </form>
      </div>
   </body>
</html>


 

Delete the record  from the database in PHP

student_delete.php

<?php
   if (isset($_POST['submit'])) {
       $id      = $_POST['id'];
       
       $connection = mysqli_connect('localhost', 'root', '', 'school');
       
       If ($connection) {
           echo "Your connection has been established";
       } else {
           die("Please try again");
       }
       
       $query  = "DELETE FROM students WHERE id= $id";
       $result = mysqli_query($connection, $query);
       if (!$result) {
           die('query FAILED' . mysqli_error());
       }
       
   }
   
   ?>
<html>
   <head>
      <title>This is demo</title>
      <meta charset="utf-8">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
   </head>
   <body>
      <div class="container">
         <h2>Student Form</h2>
         <form class="form-horizontal" action="student_delete.php" method="post">
            <div class="form-group">
               <label class="control-label col-sm-2">Id:</label>
               <div class="col-sm-7">
                  <input type="text" class="form-control" id="Id" name="id">
               </div>
            </div>
            <div class="form-group">
               <div class="col-sm-offset-2 col-sm-10">
                  <button type="submit" name="submit" class="btn btn-success">Delete</button>
               </div>
            </div>
         </form>
      </div>
   </body>
</html>

 

 

 

 

Keywords: