-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.php
153 lines (148 loc) · 5.07 KB
/
index.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
session_start();
require_once "inc/functions.php";
$info = '';
$task = $_GET['task'] ?? 'report';
$error = $_GET['error'] ?? '0';
if('edit'==$task){
if(!hasPrivilege()){
header( 'location: /crud/index.php?task=report' );
}
}
if('delete'==$task){
if(!isAdmin()){
header( 'location: /crud/index.php?task=report' );
return;
}
$id = filter_input( INPUT_GET, 'id', FILTER_SANITIZE_STRING );
if($id>0){
deleteStudent($id);
header( 'location: /crud/index.php?task=report' );
}
}
if ( 'seed' == $task ) {
if(!isAdmin()){
header( 'location: /crud/index.php?task=report' );
return;
}
seed();
$info = "Seeding is complete";
}
$fname = '';
$lname = '';
$roll = '';
if ( isset( $_POST['submit'] ) ) {
$fname = filter_input( INPUT_POST, 'fname', FILTER_SANITIZE_STRING );
$lname = filter_input( INPUT_POST, 'lname', FILTER_SANITIZE_STRING );
$roll = filter_input( INPUT_POST, 'roll', FILTER_SANITIZE_STRING );
$id = filter_input( INPUT_POST, 'id', FILTER_SANITIZE_STRING );
if($id){
//update the existing student
if ( $fname != '' && $lname != '' && $roll != '' ) {
$result = updateStudent($id, $fname, $lname, $roll);
if ( $result ) {
header( 'location: /crud/index.php?task=report' );
} else {
$error = 1;
}
}
}else{
//add a new student
if ( $fname != '' && $lname != '' && $roll != '' ) {
$result = addStudent( $fname, $lname, $roll );
if ( $result ) {
header( 'location: /crud/index.php?task=report' );
} else {
$error = 1;
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form Example</title>
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic">
<link rel="stylesheet" href="//cdn.rawgit.com/necolas/normalize.css/master/normalize.css">
<link rel="stylesheet" href="//cdn.rawgit.com/milligram/milligram/master/dist/milligram.min.css">
<style>
body {
margin-top: 30px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="column column-60 column-offset-20">
<h2>Project 2 - CRUD</h2>
<p>A sample project to perform CRUD operations using plain files and PHP</p>
<?php include_once( 'inc/templates/nav.php' ); ?>
<?php
if ( $info != '' ) {
echo "<p>{$info}</p>";
}
?>
</div>
</div>
<?php if ( '1' == $error ): ?>
<div class="row">
<div class="column column-60 column-offset-20">
<blockquote>
Duplicate Roll Number
</blockquote>
</div>
</div>
<?php endif; ?>
<?php if ( 'report' == $task ): ?>
<div class="row">
<div class="column column-60 column-offset-20">
<?php generateReport(); ?>
</div>
</div>
<?php endif; ?>
<?php if ( 'add' == $task ): ?>
<div class="row">
<div class="column column-60 column-offset-20">
<form action="/crud/index.php?task=add" method="POST">
<label for="fname">First Name</label>
<input type="text" name="fname" id="fname" value="<?php echo $fname; ?>">
<label for="lname">Last Name</label>
<input type="text" name="lname" id="lname" value="<?php echo $lname; ?>">
<label for="roll">Roll</label>
<input type="number" name="roll" id="roll" value="<?php echo $roll; ?>">
<button type="submit" class="button-primary" name="submit">Save</button>
</form>
</div>
</div>
<?php endif; ?>
<?php
if ( 'edit' == $task ):
$id = filter_input( INPUT_GET, 'id', FILTER_SANITIZE_STRING );
$student = getStudent( $id );
if ( $student ):
?>
<div class="row">
<div class="column column-60 column-offset-20">
<form method="POST">
<input type="hidden" value="<?php echo $id ?>" name="id">
<label for="fname">First Name</label>
<input type="text" name="fname" id="fname" value="<?php echo $student['fname']; ?>">
<label for="lname">Last Name</label>
<input type="text" name="lname" id="lname" value="<?php echo $student['lname']; ?>">
<label for="roll">Roll</label>
<input type="number" name="roll" id="roll" value="<?php echo $student['roll']; ?>">
<button type="submit" class="button-primary" name="submit">Update</button>
</form>
</div>
</div>
<?php
endif;
endif;
?>
</div>
<script type="text/javascript" src="assets/js/script.js"></script>
</body>
</html>