Skip to content

Commit a7e0e30

Browse files
initial code push
1 parent 678d9cc commit a7e0e30

File tree

10 files changed

+690
-0
lines changed

10 files changed

+690
-0
lines changed

Dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM ubuntu:18.04
2+
ENV DEBIAN_FRONTEND=noninteractive
3+
4+
RUN apt-get update
5+
RUN apt-get install -y mysql-server libapache2-mod-php git \
6+
php7.2 \
7+
php7.2-mysqli
8+
9+
RUN a2enmod mpm_prefork && a2enmod php7.2
10+
11+
WORKDIR /var/www/html/
12+
RUN rm -rfv index.html
13+
14+
RUN git clone https://github.com/HFX-Co/docker-challenge . && \
15+
rm -rfv .git/ && \
16+
rm -rfv Dockerfile
17+
18+
RUN chown www-data:www-data /var/www/html -R
19+
RUN chmod 755 /var/www/html
20+
21+
CMD ["bash", "-c", "/bin/bash run.sh"]

admin_dashboard.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Dashboard</title>
6+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
7+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
8+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.js"></script>
9+
<style type="text/css">
10+
.wrapper{
11+
width: 650px;
12+
margin: 0 auto;
13+
}
14+
.page-header h2{
15+
margin-top: 0;
16+
}
17+
table tr td:last-child a{
18+
margin-right: 15px;
19+
}
20+
</style>
21+
<script type="text/javascript">
22+
$(document).ready(function(){
23+
$('[data-toggle="tooltip"]').tooltip();
24+
});
25+
</script>
26+
</head>
27+
<body>
28+
<div class="wrapper">
29+
<div class="container-fluid">
30+
<div class="row">
31+
<div class="col-md-12">
32+
<div class="page-header clearfix">
33+
<h1>Internal Portal</h1><br><br><br>
34+
<h2 class="pull-left">Employees Details</h2>
35+
<a href="create.php" class="btn btn-success pull-right">Add New Employee</a>
36+
</div>
37+
<?php
38+
// Include config file
39+
require_once "config.php";
40+
41+
// Attempt select query execution
42+
$sql = "SELECT * FROM employees";
43+
if($result = mysqli_query($link, $sql)){
44+
if(mysqli_num_rows($result) > 0){
45+
echo "<table class='table table-bordered table-striped'>";
46+
echo "<thead>";
47+
echo "<tr>";
48+
echo "<th>#</th>";
49+
echo "<th>Name</th>";
50+
echo "<th>Address</th>";
51+
echo "<th>Salary</th>";
52+
echo "<th>Action</th>";
53+
echo "</tr>";
54+
echo "</thead>";
55+
echo "<tbody>";
56+
while($row = mysqli_fetch_array($result)){
57+
echo "<tr>";
58+
echo "<td>" . $row['id'] . "</td>";
59+
echo "<td>" . $row['name'] . "</td>";
60+
echo "<td>" . $row['address'] . "</td>";
61+
echo "<td>" . $row['salary'] . "</td>";
62+
echo "<td>";
63+
echo "<a href='read.php?id=". $row['id'] ."' title='View Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
64+
echo "<a href='update.php?id=". $row['id'] ."' title='Update Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>";
65+
echo "<a href='delete.php?id=". $row['id'] ."' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
66+
echo "</td>";
67+
echo "</tr>";
68+
}
69+
echo "</tbody>";
70+
echo "</table>";
71+
// Free result set
72+
mysqli_free_result($result);
73+
} else{
74+
echo "<p class='lead'><em>No records were found.</em></p>";
75+
}
76+
} else{
77+
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
78+
}
79+
80+
// Close connection
81+
mysqli_close($link);
82+
?>
83+
</div>
84+
</div>
85+
</div>
86+
</div>
87+
</body>
88+
</html>

config.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
ini_set('display_errors', 1);
4+
ini_set('display_startup_errors', 1);
5+
error_reporting(E_ALL);
6+
7+
8+
$host = "127.0.0.1:3306";
9+
$user = "admin";
10+
$pass = "adminspassword";
11+
$db = "demo";
12+
13+
$link = mysqli_connect($host, $user, $pass, $db);
14+
15+
16+
?>

create.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
ini_set('display_errors', 1);
4+
ini_set('display_startup_errors', 1);
5+
error_reporting(E_ALL);
6+
7+
// Include config file
8+
require_once "config.php";
9+
10+
// Define variables and initialize with empty values
11+
$name = $address = $salary = "";
12+
$name_err = $address_err = $salary_err = "";
13+
14+
// Processing form data when form is submitted
15+
if($_SERVER["REQUEST_METHOD"] == "POST"){
16+
// Validate name
17+
$input_name = trim($_POST["name"]);
18+
if(empty($input_name)){
19+
$name_err = "Please enter a name.";
20+
} elseif(!filter_var($input_name, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
21+
$name_err = "Please enter a valid name.";
22+
} else{
23+
$name = $input_name;
24+
}
25+
26+
// Validate address
27+
$input_address = trim($_POST["address"]);
28+
if(empty($input_address)){
29+
$address_err = "Please enter an address.";
30+
} else{
31+
$address = $input_address;
32+
}
33+
34+
// Validate salary
35+
$input_salary = trim($_POST["salary"]);
36+
if(empty($input_salary)){
37+
$salary_err = "Please enter the salary amount.";
38+
} elseif(!ctype_digit($input_salary)){
39+
$salary_err = "Please enter a positive integer value.";
40+
} else{
41+
$salary = $input_salary;
42+
}
43+
44+
// Check input errors before inserting in database
45+
if(empty($name_err) && empty($address_err) && empty($salary_err)){
46+
// Prepare an insert statement
47+
$sql = "INSERT INTO employees (name, address, salary) VALUES (?, ?, ?)";
48+
49+
50+
if($stmt = mysqli_prepare($link, $sql)){
51+
// Bind variables to the prepared statement as parameters
52+
mysqli_stmt_bind_param($stmt, "sss", $param_name, $param_address, $param_salary);
53+
54+
// Set parameters
55+
$param_name = $name;
56+
$param_address = $address;
57+
$param_salary = $salary;
58+
59+
// Attempt to execute the prepared statement
60+
if(mysqli_stmt_execute($stmt)){
61+
// Records created successfully. Redirect to landing page
62+
header("location: index.php");
63+
exit();
64+
} else{
65+
echo "Something went wrong. Please try again later.";
66+
}
67+
}
68+
69+
// Close statement
70+
mysqli_stmt_close($stmt);
71+
}
72+
73+
// Close connection
74+
mysqli_close($link);
75+
}
76+
?>
77+
78+
<!DOCTYPE html>
79+
<html lang="en">
80+
<head>
81+
<meta charset="UTF-8">
82+
<title>Create Record</title>
83+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
84+
<style type="text/css">
85+
.wrapper{
86+
width: 500px;
87+
margin: 0 auto;
88+
}
89+
</style>
90+
</head>
91+
<body>
92+
<div class="wrapper">
93+
<div class="container-fluid">
94+
<div class="row">
95+
<div class="col-md-12">
96+
<div class="page-header">
97+
<h2>Create Record</h2>
98+
</div>
99+
<p>Please fill this form and submit to add employee record to the database.</p>
100+
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
101+
<div class="form-group <?php echo (!empty($name_err)) ? 'has-error' : ''; ?>">
102+
<label>Name</label>
103+
<input type="text" name="name" class="form-control" value="<?php echo $name; ?>">
104+
<span class="help-block"><?php echo $name_err;?></span>
105+
</div>
106+
<div class="form-group <?php echo (!empty($address_err)) ? 'has-error' : ''; ?>">
107+
<label>Address</label>
108+
<textarea name="address" class="form-control"><?php echo $address; ?></textarea>
109+
<span class="help-block"><?php echo $address_err;?></span>
110+
</div>
111+
<div class="form-group <?php echo (!empty($salary_err)) ? 'has-error' : ''; ?>">
112+
<label>Salary</label>
113+
<input type="text" name="salary" class="form-control" value="<?php echo $salary; ?>">
114+
<span class="help-block"><?php echo $salary_err;?></span>
115+
</div>
116+
<input type="submit" class="btn btn-primary" value="Submit">
117+
<a href="index.php" class="btn btn-default">Cancel</a>
118+
</form>
119+
</div>
120+
</div>
121+
</div>
122+
</div>
123+
</body>
124+
</html>

delete.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
// Process delete operation after confirmation
3+
if(isset($_POST["id"]) && !empty($_POST["id"])){
4+
// Include config file
5+
require_once "config.php";
6+
7+
// Prepare a delete statement
8+
$sql = "DELETE FROM employees WHERE id = ?";
9+
10+
if($stmt = mysqli_prepare($link, $sql)){
11+
// Bind variables to the prepared statement as parameters
12+
mysqli_stmt_bind_param($stmt, "i", $param_id);
13+
14+
// Set parameters
15+
$param_id = trim($_POST["id"]);
16+
17+
// Attempt to execute the prepared statement
18+
if(mysqli_stmt_execute($stmt)){
19+
// Records deleted successfully. Redirect to landing page
20+
header("location: index.php");
21+
exit();
22+
} else{
23+
echo "Oops! Something went wrong. Please try again later.";
24+
}
25+
}
26+
27+
// Close statement
28+
mysqli_stmt_close($stmt);
29+
30+
// Close connection
31+
mysqli_close($link);
32+
} else{
33+
// Check existence of id parameter
34+
if(empty(trim($_GET["id"]))){
35+
// URL doesn't contain id parameter. Redirect to error page
36+
header("location: error.php");
37+
exit();
38+
}
39+
}
40+
?>
41+
<!DOCTYPE html>
42+
<html lang="en">
43+
<head>
44+
<meta charset="UTF-8">
45+
<title>View Record</title>
46+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
47+
<style type="text/css">
48+
.wrapper{
49+
width: 500px;
50+
margin: 0 auto;
51+
}
52+
</style>
53+
</head>
54+
<body>
55+
<div class="wrapper">
56+
<div class="container-fluid">
57+
<div class="row">
58+
<div class="col-md-12">
59+
<div class="page-header">
60+
<h1>Delete Record</h1>
61+
</div>
62+
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
63+
<div class="alert alert-danger fade in">
64+
<input type="hidden" name="id" value="<?php echo trim($_GET["id"]); ?>"/>
65+
<p>Are you sure you want to delete this record?</p><br>
66+
<p>
67+
<input type="submit" value="Yes" class="btn btn-danger">
68+
<a href="index.php" class="btn btn-default">No</a>
69+
</p>
70+
</div>
71+
</form>
72+
</div>
73+
</div>
74+
</div>
75+
</div>
76+
</body>
77+
</html>

error.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Error</title>
6+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
7+
<style type="text/css">
8+
.wrapper{
9+
width: 750px;
10+
margin: 0 auto;
11+
}
12+
</style>
13+
</head>
14+
<body>
15+
<div class="wrapper">
16+
<div class="container-fluid">
17+
<div class="row">
18+
<div class="col-md-12">
19+
<div class="page-header">
20+
<h1>Invalid Request</h1>
21+
</div>
22+
<div class="alert alert-danger fade in">
23+
<p>Sorry, you've made an invalid request. Please <a href="index.php" class="alert-link">go back</a> and try again.</p>
24+
</div>
25+
</div>
26+
</div>
27+
</div>
28+
</div>
29+
</body>
30+
</html>

0 commit comments

Comments
 (0)