Ex No: 10 Demonstrate the Database - Insert Operation
Aim: To write a PHP script to demonstrate database insert operation.
Algorithm
Step1: Create a database with name stud_db and create a table stud_table to store roll_no and name of the student.
Step2: Create a Database Class with constructor to insert data into the database.
Step3: Desing a webpage to get number of students n and insert the values into the database.
Step4: Check if data is available to be inserted.
   A) Include the database class file.
   B) Create a new database object get number of students, students roll number and students name.
   C) Call the function using object to insert the data in the database.
   D) After Inserting the data display success message.
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 add_student($no,$roll,$name)
 {
  for($i=0;$i<$no;$i++)
  {
   $qry = "INSERT INTO stud_table(roll_no, stud_name) VALUES ('$roll[$i]','$name[$i]')";
   $res = mysqli_query($this->db_con,$qry) or die(mysqli_error($this->db_con));
   if($res==FALSE)
    return $res;
  }
  return $res;
 }

 public function __destruct()
 {
  mysqli_close($this->db_con);
 }
}
?>
            
ex10.php
            
  
<html>
<head>
 <meta charset="utf-8">
 <title>Student Management System</title>
 <script>
  function add_stud()
  {
   val = document.getElementById("stud_num").value;
   tr = "";
   for(i=0;i<val;i++)
   {
    td_roll = '<tr><td>Roll No:</td><td><input type="text" name="roll[]" required="true"/></td></tr>';
    td_name = '<tr><td>Student Name:</td><td><input type="text" name="studname[]" required="true"/></td></tr>';
    tr = tr+td_roll+td_name;
   }
   tr_btn = '<tr><td><input type="submit" value="Add Student"/></td><td><input type="reset"/></td></tr>';
   tr = tr + tr_btn;
   document.getElementById("stud_add").innerHTML = tr;
  }
 </script>
 </head>
 <body> 
 <?php
  $style="";
  $resstyle="display: none;";
  if($_SERVER["REQUEST_METHOD"] == "POST")
  {
   include("db_class.php");
   $db_obj = new Database();
   $stud_num = $_POST["stud_num"];
   $roll = $_POST["roll"];
   $studname = $_POST["studname"];
   
   $res = $db_obj->add_student($stud_num,$roll,$studname);
   if($res==TRUE)
   {
   	$style = "display: none;";
   	$resstyle="";
   }   
  }
 ?>
 <div style="<?php echo $resstyle; ?>">
  <h1>Student Details Added Successfully</h1>
 </div>
 <div style="<?php echo $style;?>">   
 <h1><u>Add Student Details</u></h1>
 <form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>">
 <table>
  <tr>
   <td>Enter Number of Students: </td>
   <td><input type="text" name="stud_num" id="stud_num"/> <input type="button" value="Add" onclick="add_stud()" /></td>
  </tr>
 </table>
 <table id="stud_add">
  
 </table> 
 </form>
 </div>  
 </body>
</html>
            
Result: Thus a database insert operation is demonstrated using a webpage with php script.