-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
158 lines (143 loc) · 4.5 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
154
155
156
157
158
<!doctype html>
<?php
require 'configure.php';
$ID_student = "";
$fname = "";
$lname = "";
$phone = "";;
$email = "";
$status = "";
$start_date = "";
$end_date = "";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
//connect to mysql database
try {
$connect = mysqli_connect('localhost', 'root', '', 'rutgers');
}
catch(MySQLi_Sql_Exception $ex){
echo("error in connecting");
}
//get data from the form
function getData()
{
$data = array();
$data[0]=$_POST['ID_student'];
$data[1]=$_POST['fname'];
$data[2]=$_POST['lname'];
$data[3]=$_POST['phone'];
$data[4]=$_POST['email'];
$data[5]=$_POST['status'];
$data[6]=$_POST['start_date'];
$data[7]=$_POST['end_date'];
return $data;
}
//search
if(isset($_POST['search']))
{
$info = getData();
$search_query="SELECT * FROM students WHERE ID_student = '$info[0]' || fname = '$info[1]' || lname = '$info[2]' || phone = '$info[3]' || email = '$info[4]' || status = '$info[5]' || start_date = '$info[6]' || end_date = '$info[7]'";
$search_result=mysqli_query($connect, $search_query);
if($search_result)
{
if(mysqli_num_rows($search_result))
{
while($rows = mysqli_fetch_array($search_result))
{
$ID_student = $rows['ID_student'];
$fname = $rows['fname'];
$lname = $rows['lname'];
$phone = $rows['phone'];
$email = $rows['email'];
$status = $rows['status'];
$start_date = $rows['start_date'];
$end_date = $rows['end_date'];
}
}else {
echo("no data are available");
}
} else {
echo("result error");
}
}
//insert
if(isset($_POST['insert'])){
$info = getData();
$insert_query="INSERT INTO students(fname, lname, phone, email, status, start_date, end_date) VALUES ('$info[1]','$info[2]','$info[3]','$info[4]', '$info[5]', '$info[6]', '$info[7]')";
try{
$insert_result=mysqli_query($connect, $insert_query);
if($insert_result)
{
if(mysqli_affected_rows($connect)> 0){
echo("data inserted successfully");
}else{
echo("data are not inserted");
}
}
}catch(Exception $ex){
echo("error inserted".$ex->getMessage());
}
}
//delete
if(isset($_POST['delete'])){
$info = getData();
$delete_query = "DELETE FROM `students` WHERE ID_student = '$info[0]'";
try{
$delete_result = mysqli_query($connect, $delete_query);
if($delete_result){
if(mysqli_affected_rows($connect)> 0)
{
echo("data deleted");
}else{
echo("data not deleted");
}
}
}catch(Exception $ex){
echo("error in delete".$ex->getMessage());
}
}
//edit
if(isset($_POST['update'])){
$info = getData();
$update_query="UPDATE `students` SET `fname`='$info[1]', lname='$info[2]', phone='$info[3]', email='$info[4]', status='$info[5]',start_date='$info[6]',end_date='$info[7]' WHERE ID_student = '$info[0]'";
try{
$update_result=mysqli_query($connect, $update_query);
if($update_result){
if(mysqli_affected_rows($connect)> 0){
echo("data updated");
}else{
echo("data not updated");
}
}
}catch(Exception $ex){
echo("error in update".$ex->getMessage());
}
}
?>
<html>
<head>
<meta charset="utf-8">
<title>Perform insert, update, delete, and search operations on records in database table!</title>
<link rel="stylesheet" type="text/css" href="style.css" >
</head>
<body>
<form method ="post" action="index.php">
<div class="input-group">
<input type="number" name="ID_student" placeholder="ID (Use For Search Only)" value="<?php echo($ID_student);?>"><br><br>
<input type="text" name="fname" placeholder="First Name" value="<?php echo($fname);?>"><br><br>
<input type="text" name="lname" placeholder="Last Name" value="<?php echo($lname);?>"><br><br>
<input type="text" name="phone" placeholder="Phone #" value="<?php echo($phone);?>"><br><br>
<input type="text" name="email" placeholder="[email protected]" value="<?php echo($email);?>"><br><br>
<p>Do not search by status!</p> <!-- Do not search by status as it yields no useful results! -->
<input type="text" name="status" placeholder="Status" value="<?php echo($status);?>"><br><br>
<input type="text" name="start_date" placeholder="Start Date" value="<?php echo($start_date);?>"><br><br>
<input type="text" name="end_date" placeholder="End Date" value="<?php echo($end_date);?>"><br><br>
<div>
<input type="submit" name="insert" value="Add" class="btn">
<input type="submit" name="delete" value="Delete" class="btn">
<input type="submit" name="update" value="Update" class="btn">
<input type="submit" name="search" value="Find" class="btn">
</div>
</div>
</form>
</body>
</html>