From 02884d29e4f5aea71364a203dcaecd53600d8aa4 Mon Sep 17 00:00:00 2001 From: Sudipto Mallick Date: Thu, 8 Feb 2024 11:08:33 +0000 Subject: Complete PHP assignments --- mysql-php/code/a4.php | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 mysql-php/code/a4.php (limited to 'mysql-php/code/a4.php') diff --git a/mysql-php/code/a4.php b/mysql-php/code/a4.php new file mode 100644 index 0000000..baa1b45 --- /dev/null +++ b/mysql-php/code/a4.php @@ -0,0 +1,107 @@ +set_charset('utf8mb4'); + $dbh->query('CREATE DATABASE IF NOT EXISTS COMPANY_DB'); + $dbh->select_db('COMPANY_DB'); + $dbh->query('CREATE TABLE IF NOT EXISTS `SALARY` (`EMP_ID` INT PRIMARY KEY AUTO_INCREMENT, `EMP_NAME` VARCHAR(255), `BASIC_PAY` DOUBLE NOT NULL, `HRA` DOUBLE, `DA` DOUBLE, `PROFESSIONAL_TAX` DOUBLE)'); + $result = $dbh->query('SELECT COUNT(*) AS count FROM `SALARY`'); + $row = $result->fetch_assoc(); + if ($row['count'] == 0) { + $dbh->query('INSERT INTO `SALARY` (`EMP_NAME`, `BASIC_PAY`) VALUES + ("Yu Jae-hoon", 70000), + ("Meng Cilin", 65000), + ("Mike Smith", 60000), + ("Natsukashii Morimoto", 55000) + '); + } + return $dbh; +} + + +function html_prologue($title) { +?> + +<?php echo $title; ?> + + +

Operation failed

+

Reason:

+query('SELECT * FROM `SALARY`'); + ?> +

Employee Salary Records

+ + +fetch_assoc()) { + echo ''; +} ?> +
Employee NameBasic PayHRADAProfessional Tax
', $row['EMP_NAME'], '', number_format(+$row['BASIC_PAY'], 2), '', number_format(+$row['HRA'], 2), '', number_format(+$row['DA'], 2), '', number_format(+$row['PROFESSIONAL_TAX'], 2), '
+ +
+

Enter Salary Components

+ + + + + +
%
%
%
+
+ query('SELECT * FROM `SALARY`'); + while ($row = $result->fetch_assoc()) { + $hra = $row['BASIC_PAY'] * ($hra_percentage / 100); + $da = $row['BASIC_PAY'] * ($da_percentage / 100); + $ptax = $row['BASIC_PAY'] * ($professional_tax / 100); + $stmt = $dbh->prepare('UPDATE `SALARY` SET `HRA` = ?, `DA` = ?, `PROFESSIONAL_TAX` = ? WHERE `EMP_ID` = ?'); + $stmt->bind_param('dddi', $hra, $da, $ptax, $row['EMP_ID']); + $stmt->execute(); + } + display_records($dbh); +} + +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + try { + $dbh = connect_to_database(); + calculate_salary($dbh); + $dbh->close(); + } catch (mysqli_sql_exception $e) { + display_failure($e->getMessage()); + } +} else { + show_salary_form(); +} -- cgit 1.4.1-2-gfad0