Guest User

Untitled

a guest
Dec 4th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. <?php
  2. class Student
  3. {
  4. private $conn;
  5. function __construct() {
  6. session_start();
  7. $servername = "localhost";
  8. $username = "root";
  9. $password = "";
  10. $dbname = "crud";
  11.  
  12. // Create connection
  13. $conn = new mysqli($servername, $username, $password, $dbname);
  14. // Check connection
  15. if ($conn->connect_error) {
  16. die("Connection failed: " . $conn->connect_error);
  17. }else{
  18. $this->conn=$conn;
  19. }
  20.  
  21. }
  22.  
  23.  
  24. public function student_list(){
  25.  
  26. $sql = "SELECT * FROM students ORDER BY student_id asc ";
  27. $result= $this->conn->query($sql);
  28. return $result;
  29. }
  30.  
  31. public function create_student_info($post_data=array()){
  32.  
  33. if(isset($post_data['create_student'])){
  34. $student_name= mysqli_real_escape_string($this->conn,trim($post_data['student_name']));
  35. $email_address= mysqli_real_escape_string($this->conn,trim($post_data['email_address']));
  36. $gender= mysqli_real_escape_string($this->conn,trim($post_data['gender']));
  37. $contact= mysqli_real_escape_string($this->conn,trim($post_data['contact']));
  38. $country= mysqli_real_escape_string($this->conn,trim($post_data['country']));
  39.  
  40. $sql="INSERT INTO students (student_name, email_address, contact,country,gender) VALUES ('$student_name', '$email_address', '$contact','$country','$gender')";
  41.  
  42. $result= $this->conn->query($sql);
  43.  
  44. if($result){
  45.  
  46. $_SESSION['message']="Successfully Created Student Info";
  47.  
  48. header('Location: index.php');
  49. }
  50.  
  51. unset($post_data['create_student']);
  52. }
  53.  
  54.  
  55. }
  56.  
  57. public function view_student_by_student_id($id){
  58. if(isset($id)){
  59. $student_id= mysqli_real_escape_string($this->conn,trim($id));
  60.  
  61. $sql="Select * from students where student_id='$student_id'";
  62.  
  63. $result= $this->conn->query($sql);
  64.  
  65. return $result->fetch_assoc();
  66.  
  67. }
  68. }
  69.  
  70.  
  71. public function update_student_info($post_data=array()){
  72. if(isset($post_data['update_student'])&& isset($post_data['id'])){
  73.  
  74. $student_name= mysqli_real_escape_string($this->conn,trim($post_data['student_name']));
  75. $email_address= mysqli_real_escape_string($this->conn,trim($post_data['email_address']));
  76. $gender= mysqli_real_escape_string($this->conn,trim($post_data['gender']));
  77. $contact= mysqli_real_escape_string($this->conn,trim($post_data['contact']));
  78. $country= mysqli_real_escape_string($this->conn,trim($post_data['country']));
  79. $student_id= mysqli_real_escape_string($this->conn,trim($post_data['id']));
  80.  
  81. $sql="UPDATE students SET student_name='$student_name',email_address='$email_address',contact='$contact',country='$country',gender='$gender' WHERE student_id =$student_id";
  82.  
  83. $result= $this->conn->query($sql);
  84.  
  85. if($result){
  86. $_SESSION['message']="Successfully Updated Student Info";
  87. }
  88. unset($post_data['update_student']);
  89. }
  90. }
  91.  
  92. public function delete_student_info_by_id($id){
  93.  
  94. if(isset($id)){
  95. $student_id= mysqli_real_escape_string($this->conn,trim($id));
  96.  
  97. $sql="DELETE FROM students WHERE student_id =$student_id";
  98. $result= $this->conn->query($sql);
  99.  
  100. if($result){
  101. $_SESSION['message']="Successfully Deleted Student Info";
  102.  
  103. }
  104. }
  105. header('Location: index.php');
  106. }
  107. function __destruct() {
  108. mysqli_close($this->conn);
  109. }
  110.  
  111. }
  112. ?>
Add Comment
Please, Sign In to add comment