-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
executable file
·81 lines (66 loc) · 1.94 KB
/
index.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
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
#!/usr/bin/python3
import cgi, pymysql
form = cgi.FieldStorage()
form_name = form.getvalue("name")
form_color = form.getvalue("color")
form_cats_or_dogs = form.getvalue("cats_or_dogs")
# Print HTML
print("Content-type:text/html\r\n\r\n")
print('''
<html>
<head>
<style>
body {
font: 22px Arial, sans-serif;
}
</style>
</head>
<body>
<center>
<h1>My Demo</h1>
''')
db = pymysql.connect("localhost","root","thisisdemo","demo")
cursor = db.cursor()
#query_sql = "SELECT * FROM demo"
#cursor.execute(query_sql)
#records = cursor.fetchall()
#query_result = "Data:<br>"
#for row in records:
# query_result = query_result + row[0] + " " * 3 + row[1] + " " * 3 + row[2] + "<br>"
# Insert data if all fields are non-empty
if all([form_name, form_color, form_cats_or_dogs]):
select_sql = "SELECT * FROM demo where name = '" + form_name + "'"
cursor.execute(select_sql)
rows_num = cursor.rowcount
if rows_num > 0:
print("<p>" + form_name + " has been exist already!")
else:
insert_sql = "INSERT INTO demo (name, color, cats_or_dogs) VALUES ('" + form_name + "', '" + form_color + "', '" + form_cats_or_dogs + "')"
cursor.execute(insert_sql)
db.commit()
db.close()
print('''
<form action="/cgi-bin/index.py" method="post">
Name: <input type="text" name="name" autofocus>
<p>Favourite Color:
<select name="color" autofocus>
<option value="Red">Red</option>
<option value="Orange">Orange</option>
<option value="Yellow">Yellow</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Indigo">Indigo</option>
<option value="Violet">Violet</option>
</select><br>
<p>Cats or Dogs
<input type="radio" name="cats_or_dogs" value="Cats">Cats
<input type="radio" name="cats_or_dogs" value="Dogs">Dogs
<p><input type="submit" value="Submit">
</form>
<button class="button" onClick="window.open('/cgi-bin/data.py');">
<span class="icon">Display Data</span>
</button>
</center>
</body>
</html>
''')