Add New Employee
Fill out the form to add a new employee to the system.
prepare($sql)) { $stmt->bindParam(':username', trim($_POST['username']), PDO::PARAM_STR); if ($stmt->execute()) { if ($stmt->rowCount() == 1) { $username_err = 'This username is already taken.'; } else { $username = trim($_POST['username']); } } else { echo 'Oops! Something went wrong. Please try again later.'; } unset($stmt); } } // Validate password if (empty(trim($_POST['password']))) { $password_err = 'Please enter a password.'; } elseif (strlen(trim($_POST['password'])) < 6) { $password_err = 'Password must have at least 6 characters.'; } else { $password = trim($_POST['password']); } // Validate role if (empty($_POST['role'])) { $role_err = 'Please select a role.'; } else { $role = $_POST['role']; } // Check input errors before inserting in database if (empty($username_err) && empty($password_err) && empty($role_err)) { $sql = 'INSERT INTO users (username, password, role) VALUES (:username, :password, :role)'; if ($stmt = db()->prepare($sql)) { $stmt->bindParam(':username', $username, PDO::PARAM_STR); $stmt->bindParam(':password', $hashed_password, PDO::PARAM_STR); $stmt->bindParam(':role', $role, PDO::PARAM_STR); // Hash password $hashed_password = password_hash($password, PASSWORD_DEFAULT); if ($stmt->execute()) { $success_msg = 'Employee added successfully!'; // Clear form fields $username = $password = $role = ''; } else { echo 'Oops! Something went wrong. Please try again later.'; } unset($stmt); } } } ?>
Fill out the form to add a new employee to the system.