-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit-top-ten.php
127 lines (114 loc) · 4.04 KB
/
edit-top-ten.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
<?php
if (!isset($_COOKIE['first_name']))
{
// Need the functions:
require('./inc/login_functions.inc.php');
redirect_user();
}
require_once('./inc/header.php'); ?>
<?php
//Handle Top Ten form
$submitted = isset($_POST['submit']);
if($submitted)
{
// Make sure every field has something in it
$blankField = false;
for($i = 1; $i <= 10; $i++)
{
if($_POST[$i] == "")
{
$blankField = true;
}
}
// If everything looks good, submit to the databasen
if(!$blankField)
{
for($i = 1; $i <= 10; $i++)
{
$sql = "UPDATE top_ten
SET item = '" . mysqli_real_escape_string($connection, $_POST[$i]) .
"' WHERE rank=$i";
//Send the query to the database
$result = @mysqli_query($connection, $sql);
if(!$result)
{
echo "<p class='error'>There was a problem with the database!</p>";
}
}
}
}
?>
<section id="content">
<section class="section page-heading animate-onscroll">
<button class="logout"><a href="./logout.php">Log Out</a></button>
<h1>Edit Top Ten List</h1>
</section>
<!-- Section -->
<section class="section full-width-bg gray-bg animate-onscroll">
<button class="admin"><a href="admin.php">Back to Admin</a></button>
<div class="row">
<div class="col-md-3 col-xs-0"></div>
<div class="col-md-6 col-xs-12 text-center">
<h3>Edit Top Ten List</h3>
<p>Edit any or all of the fields below, then click save.</p>
<p><button id="restore-top-ten">Restore Defaults</button></p>
<?php
if($submitted)
{
if($blankField)
{
echo '<p class="error">Please enter a needed item for each field.</p>';
}
else
{
echo '<p class="success">List successfully updated!</p>';
}
}
?>
<form action="" method="post">
<?php // Loop to display top ten items from the database
//Define the select query
$sql = "SELECT * FROM top_ten ORDER BY rank";
//Send the query to the database
$result = @mysqli_query($connection, $sql);
$i = 1;
//Process the rows
while($row = mysqli_fetch_assoc($result))
{
echo "<input type='text' id='$i' name='$i' value='";
if(isset($_POST['submit']))
{
echo $_POST[$i];
}
else
{
echo $row['item'];
}
echo "'>";
$i++;
}
?>
<input type="submit" value="Save Top Ten" name="submit">
</form>
</div>
<div class="col-md-3 col-xs-0"></div>
</div>
</section>
</section>
<script>
$(document).ready(function() {
$('#restore-top-ten').click(function(){
$('#1').val("Soup – condensed and ready to eat");
$('#2').val("Canned vegetables");
$('#3').val("Canned tomato products");
$('#4').val("Canned fruit");
$('#5').val("Canned proteins – SPAM, tuna, chicken");
$('#6').val("Ready to eat meals – chili, Chef Boyardee");
$('#7').val("Canned or bagged beans");
$('#8').val("Toiletries");
$('#9').val("Diapers and Formula");
$('#10').val("Office supplies - paper, pens, garbage bags");
});
});
</script>
<?php require_once('./inc/footer.php'); ?>