Ex No: 13 Demonstrate the Database - Update Operation
Aim: To write a PHP script to demonstrate database Update operation.
Algorithm
Step1: Use the stud_db database with stud_table.
Step2: Display all the studnet data from the database.
Step3: Get Roll No and new name to be updated.
Step4: Check if Roll No is present for Updation.
   A) Include the database class file.
   B) Create a new database object.
   C) Get roll no and new studnet name for updation.
   D) Call the update student function using object to update the data in the database.
   E) If student data is found update name and display updated details.
   F) else display "student data not found".
db_class.php
<?php
 class Database
 {
  var $hostadd;
  var $usr;
  var $pwd;
  var $DB;
  var $db_con;
  
  public function __construct()
  {
   $this->hostadd = "127.0.0.1";
   $this->usr = "root";
   $this->pwd = "";
   $this->DB= "stud_db";
   $this->db_con = mysqli_connect($this->hostadd,$this->usr,$this->pwd,$this->DB);
   
   if(mysqli_connect_errno())
   {
   	header("Location:error.php");
   	return mysqli_connect_errno();
   	exit();
   }
   else
   {
   	return 1;
   } 
  }
  
  public function updt_stud($roll_no,$stud_name)
  {
   $qry = "update stud_table set stud_name='$stud_name' where roll_no=$roll_no";
   $res = mysqli_query($this->db_con,$qry) or die("error in query");
   $numrows = mysqli_affected_rows($this->db_con);	
   $newres = "";	
   if($numrows==1)
   {
   	$newres = $this->select_stud();
   }
   return $newres;
  }
	
  public function select_stud()
  {
   $qry = "select roll_no, stud_name from stud_table";
   $res = mysqli_query($this->db_con,$qry) or die("Error in query");
   $numrows = mysqli_num_rows($res);
   $elm = "";
   if($numrows>0)
   {
    while($row = mysqli_fetch_row($res))
    {
     $elm = $elm."<tr><td>$row[0]</td><td>$row[1]</td></tr>";
    }
   }
   return $elm;	
  }  
	
  public function __destruct()
  {
   mysqli_close($this->db_con);
  }
 }
?>
            
ex11.php
            
<html>
<head>
 <meta charset="utf-8">
 <title>Student Management System</title> 
 </head>
 <body> 
 <?php
  include("db_class.php");
  $style = $editres = $res = "";
  $editstyle ="display:none;";
  $db_obj = new Database();
  if($_SERVER["REQUEST_METHOD"] == "POST")
  {   
   if(isset($_POST["stud_roll"]))//if check post
   {
    $stud_roll = $_POST["stud_roll"];
    $stud_name = $_POST["stud_name"];
    $editres = $db_obj->updt_stud($stud_roll,$stud_name);   
    if($editres!="")//if check edit result
    {
     $style = "display: none;";
     $editstyle="";
    }
    else
    {
     $res = $db_obj->select_stud();
  ?>

  <script>
  	alert("Student data not found");
  </script>

  <?php		
	}//end if edit result
   }//end if check post
  }
  else
  {   
   $res = $db_obj->select_stud();	
  }
  unset($db_obj);
 ?>
 
 <div style="<?php echo $editstyle; ?>">
  <h1>Student Data After Updation</h1>
  <table>
  <?php echo $editres; ?>
  </table>
 </div> 
 
 
 <div style="<?php echo $style;?>">   
 <h1><u>Update Student Name</u></h1>
 <form method="POST" name="search" action="<?php echo $_SERVER["PHP_SELF"];?>">
 <table>
 <?php
  echo $res;
 ?>
 </table>
  <table>
  <tr>
   <td>Roll No:</td>
   <td><input type="text" name="stud_roll" required="true" /></td>
  </tr>
  <tr>
   <td>New Name:</td>
   <td><input type="text" name="stud_name" required="true" /></td>
  </tr>
  <tr>
   <td><input type="submit" value="Update"/></td>
   <td><input type="reset"/></td>
  </tr>
 </table> 
 </form>
 </div>  
 </body>
</html>
            
Result: Thus a database update operation is demonstrated using a webpage with php script.