[text] R6

Viewer

  1. <?php
  2. class EmployeeModel {
  3.     private $conn;
  4.  
  5.     public function __construct($host, $username, $password, $database) {
  6.         // Create connection
  7.         $this->conn = new mysqli($host, $username, $password, $database);
  8.  
  9.         // Check connection
  10.         if ($this->conn->connect_error) {
  11.             die("Connection failed: " . $this->conn->connect_error);
  12.         }
  13.     }
  14.  
  15.     // Method to add an employee
  16.     public function addEmployee($employee) {
  17.         $stmt = $this->conn->prepare("INSERT INTO Employee (EmployeeID, Name, DateOfBirth, Position, Department, HireDate, Salary, Email, PhoneNumber) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
  18.         $stmt->bind_param("isssssds", 
  19.             $employee['EmployeeID'],
  20.             $employee['Name'],
  21.             $employee['DateOfBirth'],
  22.             $employee['Position'],
  23.             $employee['Department'],
  24.             $employee['HireDate'],
  25.             $employee['Salary'],
  26.             $employee['Email'],
  27.             $employee['PhoneNumber']
  28.         );
  29.         $stmt->execute();
  30.         $stmt->close();
  31.     }
  32.  
  33.     // Method to view all employees
  34.     public function getAllEmployees() {
  35.         $result = $this->conn->query("SELECT * FROM Employee");
  36.         $employees = [];
  37.         while ($row = $result->fetch_assoc()) {
  38.             $employees[] = $row;
  39.         }
  40.         return $employees;
  41.     }
  42. }
  43. ?>

Editor

You can edit this paste and save as new:


File Description
  • R6
  • Paste Code
  • 01 Jul-2024
  • 1.37 Kb
You can Share it: