-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_1.php
67 lines (62 loc) · 1.68 KB
/
4_1.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
$conn = OpenCon();
function OpenCon()
{
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$db = "employees";
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn -> error);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
echo("oppkobling vellykket"."<br>");
}
return $conn;
}
function CloseCon($conn)
{
$conn -> close();
}
$average=$total=$yearError = " ";
if ($_SERVER["REQUEST_METHOD"]=="POST") {
if (empty($_POST["year"])) {
$yearError = "Skriv inn årstall";
}elseif($_POST["year"]<1 or $_POST["year"]>date("Y")){
echo "------------error------------ skriv riktig årstall";
}else{
$year = $_POST["year"];
$sql = "SELECT sum(salary) as total, count(*) as quantity FROM salaries WHERE YEAR(to_date)='".$year."'";
$query = $conn->query($sql);
$result = $query->fetch_assoc();
if($_POST["choice"]=="average"){
$average = $result["total"]/$result["quantity"];
}elseif($result["total"]==0){
$average=$total=" ";
}
else{
$total = $result["total"];
}
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
År:
<input type="number" name="year">
<span class="error"><?php echo $yearError;?></span><br>
<input type="radio" name="choice" value="average" > gjennomsnittslønn <br>
<input type="radio" name="choice" value = "total"> total lønn <br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if($average != " ") {
echo "gjennomsnittslønn i ". $year. " was ".$average;
}
if($total != " ") {
echo "total lønn i ". $year. " was ".$total;
}
$average = $total = " ";
?>