-
Notifications
You must be signed in to change notification settings - Fork 0
/
display_voting_form.py
42 lines (37 loc) · 1.25 KB
/
display_voting_form.py
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
#!/usr/local/bin/python3
from cgitb import enable
enable()
import pymysql as db
print('Content-Type: text/html')
print()
result = ''
try:
connection = db.connect(# login)
cursor = connection.cursor(db.cursors.DictCursor)
cursor.execute("""SELECT candidate_name
FROM candidates
ORDER BY candidate_name ASC""")
if cursor.rowcount == 0:
result = '<p>Sorry. There are no candidates at present.</p>'
else:
options = ''
for row in cursor.fetchall():
options += '<option value="%s">%s</option>' % (row['candidate_name'], row['candidate_name'])
result = """<form action="update_vote.py" method="post">
<select name="candidate_name">%s</select>
<input type="submit" value="Vote now">
</form>""" % (options)
cursor.close()
connection.close()
except db.Error:
result = '<p>Sorry! We are experiencing problems at the moment. Please call back later.</p>'
print("""
<!DOCTYPE html>
<html lang="en">
<head>
<title>Place your vote</title>
</head>
<body>
%s
</body>
</html>""" % (result))