-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.php
executable file
·174 lines (155 loc) · 5.48 KB
/
delete.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
session_start();
/*
* Starts the session if there is one
*/
if (!isset($_SESSION['name'])) {
/*
* If does not exist a valid session
*/
echo "Access denied!";
} else {
/*
* If there is a valid session
*/
include("session.php");
echo "<h3>Choose an ID to delete:</h3><p>";
/*
* Prints a heading in html
*/
mysql_connect("localhost", "root", "") or die("problem with connection..."); /*
* Connects to the localhost database with the user root and blank
* password, if it doesn't, stops the execution of the program
* returning "Problems with connection!"
*/
mysql_select_db("testsite");
/*
* After the connection is up, select the database to operate on
*/
$per_page = 6;
$pages_query = mysql_query("SELECT COUNT('id') FROM users");
/*
* Creates a query to count all the users in the database
*/
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
/*
* $pages will receive the number of pages required to show all the users,
* using $per_page number of users per page
* So, the result query (number total of users) dividing by users per page
* variable will result in the number of pages required to show all the users
* ceil() method will round up, giving an integer
*/
$page = (isset($_GET['page'])) ? (int) $_GET['page'] : 1;
/*
* If variable "page" form GET is set, use it
* If not, set it as 1
*/
$start = ($page - 1) * $per_page;
/*
* $start is the index that will define in the database which users will be
* showing up. For example, if the User is on page 3, and we show 6 users per
* page, then (3-1)*6 = 12
*/
$query = mysql_query("SELECT * FROM users LIMIT $start, $per_page");
/*
* Using the $start variable, this is a query to the database, selecting the
* users, from $start until $start + %per_page
* Using the example above, $start = 12, the query will be:
* SELECT * FROM users LIMIT 12, 18
* It will return the users with and id between 12 and 18, 6 users
*/
echo "<table width=\"90%\" align=center border=2>";
/*
* Creation of a centered table with width of 90% and a border of 2
*/
echo "<tr>
<td width=\"40%\" align=center bgcolor=\"FFFF00\">ID</td>
<td width=\"40%\" align=center bgcolor=\"FFFF00\">NAME</td>
<td width=\"40%\" align=center bgcolor=\"FFFF00\">EMAIL</td>
<td width=\"40%\" align=center bgcolor=\"FFFF00\">PASSWORD</td>
<tr>";
/*
* The echo above, will define the first row of the table with ID, NAME ,
* EMAIL and PASSWORD
*/
while ($row = mysql_fetch_assoc($query)) {
/*
* A while loop: while there is users returning from the query
*/
$id = $row['id'];
$name = $row['name'];
$email = $row['email'];
$password = $row['password'];
/*
* Save the user's information returned from the database
*/
echo "<tr>
<td align=center><a href=\"delete1.php?ids=$id&names=$name&emails=$email&passwords=$password\">$id</a></td>
<td>$name</td>
<td>$email</td>
<td>$password</td>
</tr>";
/*
* The echo above, will create each row, for each user, showing the ID,
* NAME, EMAIL and PASSWORD and creating a link on the ID, used to delete
* that user
*/
} // End of while loop
echo "</table>";
/*
* End of the table
*/
/*
* The next lines of code, will produce the pagination at the end of the
* table, giving the possibility to the User to surf every page
*/
$prev = $page - 1;
$next = $page + 1;
echo "<center>";
/*
* Centering the text
*/
if (!($page <= 1)) {
/*
* If the page number is not less or equal than 1
* Meaning there is a lower page that the one the User's on
*/
echo "<a href='delete.php?page=$prev'>Prev</a> ";
/*
* Create a link for the previous page
*/
}
if ($pages >= 1 && $page <= $pages) {
/*
* If the number of pages is bigger or equal to one and the page the User
* is on is lower or equal than the number or pages
*/
for ($x = 1; $x <= $pages; $x++) {
/*
* This for will create a link for each page. For example, there is
* 5 pages ($pages = 5). The variable x will increase from 1 to 5
*/
echo ($x == $page) ? '<strong><a href="?page=' . $x . '">' . $x . '</a></strong> ' : '<a href="?page=' . $x . '">' . $x . '</a> ';
/*
* For each value of $x, create a link to pageX, and if $x is the page
* the User's on, then the link will be in Bold
*/
} // End of for loop
} // End of if
if (!($page >= $pages)) {
/*
* If the page number the User's on isn't bigger or equal the number of
* total pages, then creates a Next Page link.
*/
echo "<a href='delete.php?page=$next'>Next</a>";
}
echo "</center>";
/*
* Closes the center tag
*/
mysql_close();
/*
* Closes the connection to the database
*/
}
?>