-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdate_Student.php
158 lines (154 loc) · 6.64 KB
/
Update_Student.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
<?php
# Get data to input
# NEW add student_id input
$first_name = filter_input(INPUT_POST, "first_name");
$last_name = filter_input(INPUT_POST, "last_name");
$email = filter_input(INPUT_POST, "email");
$street = filter_input(INPUT_POST, "street");
$city = filter_input(INPUT_POST, "city");
$state = filter_input(INPUT_POST, "state");
$zip = filter_input(INPUT_POST, "zip");
$phone = filter_input(INPUT_POST, "phone");
$birth_date = filter_input(INPUT_POST, "birth_date");
$sex = filter_input(INPUT_POST, "sex");
$lunch_cost = filter_input(INPUT_POST, "lunch", FILTER_VALIDATE_FLOAT);
# Create a timestamp for now
$date_entered = date('Y-m-d H:i:s');
$student_id = filter_input(INPUT_POST, "student_id", FILTER_VALIDATE_INT);
# Verify that eveything has been entered
if($first_name == null || $last_name == null || $email == null ||
$street == null || $city == null || $state == null ||
$zip == null || $phone == null || $birth_date == null ||
$sex == null || $lunch_cost == false || $student_id == null){
# Print an error if values aren't entered
$err_msg = "All Values Not Entered";
include('db_error.php');
} else {
require_once('db_connect.php');
# Create your query using : to add parameters to the statement
$query = 'UPDATE students
SET first_name = :first_name,
last_name = :last_name,
email = :email,
street = :street,
city = :city,
state = :state,
zip = :zip,
phone = :phone,
birth_date = :birth_date,
sex = :sex,
lunch_cost = :lunch_cost
WHERE student_id = :student_id';
# Create a PDOStatement object
$stm = $db->prepare($query);
# Bind values to parameters in the prepared statement
$stm->bindValue(':first_name', $first_name);
$stm->bindValue(':last_name', $last_name);
$stm->bindValue(':email', $email);
$stm->bindValue(':street', $street);
$stm->bindValue(':city', $city);
$stm->bindValue(':state', $state);
$stm->bindValue(':zip', $zip);
$stm->bindValue(':phone', $phone);
$stm->bindValue(':birth_date', $birth_date);
$stm->bindValue(':sex', $sex);
$stm->bindValue(':lunch_cost', $lunch_cost);
$stm->bindValue(':student_id', $student_id);
# Execute the query and store true or false based on success
$execute_success = $stm->execute();
$stm->closeCursor();
# If an error occurred print the error
if(!$execute_success){
print_r($stm->errorInfo()[2]);
}
}
require_once('db_connect.php');
$query_students = 'SELECT * FROM students ORDER BY student_id';
$student_statement = $db->prepare($query_students);
$student_statement->execute();
$students = $student_statement->fetchAll();
$student_statement->closeCursor();
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Update</title>
<link rel="stylesheet" type="text/css" href="main.css" />
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/v/bs4/jszip-2.5.0/dt-1.10.21/af-2.3.5/b-1.6.3/b-colvis-1.6.3/b-flash-1.6.3/b-html5-1.6.3/b-print-1.6.3/cr-1.5.2/fc-3.3.1/fh-3.1.7/kt-2.5.2/r-2.2.5/rg-1.1.2/rr-1.2.7/sc-2.0.2/sp-1.1.1/sl-1.3.1/datatables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.2/js/dataTables.buttons.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.2/js/buttons.flash.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.2/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.2/js/buttons.print.min.js"></script>
</head>
<body>
<h3>Student List</h3>
<table id="myTable" class="display nowrap dataTable dtr-inline collapsed" style="width: 100%;" role="grid" aria-describedby="example_info">
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Street</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
<th>Phone</th>
<th>BD</th>
<th>Sex</th>
<th>Entered</th>
<th>Lunch</th>
</tr>
<!-- Get an array from the DB query and cycle
through each row of data -->
<?php foreach($students as $student) : ?>
<tr>
<!-- Print out individual column data -->
<td><?php echo $student['student_id']; ?></td>
<td><?php echo $student['first_name'] . ' ' . $student['last_name']; ?></td>
<td><?php echo $student['email']; ?></td>
<td><?php echo $student['street']; ?></td>
<td><?php echo $student['city']; ?></td>
<td><?php echo $student['state']; ?></td>
<td><?php echo $student['zip']; ?></td>
<td><?php echo $student['phone']; ?></td>
<td><?php echo $student['birth_date']; ?></td>
<td><?php echo $student['sex']; ?></td>
<td><?php echo $student['date_entered']; ?></td>
<td><?php echo $student['lunch_cost']; ?></td>
</tr>
<!-- Mark the end of the foreach loop -->
<?php endforeach; ?>
</table>
<h3>Delete Student</h3>
<form action="delete_student.php" method="post"
id="delete_student_form">
<label>Student ID : </label>
<input type="text" name="student_id"><br>
<input type="submit" value="Delete Student"><br>
</form>
</body>
<script>
$(document).ready( function () {
$('#myTable')
.addClass( 'nowrap' )
.dataTable( {
responsive: true,
columnDefs: [
{ targets: [-1, -3], className: 'dt-body-right' }
],
dom: 'Bfrtip',
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print'
]
} );
} );
</script>
</html>