【发布时间】:2022-01-28 00:18:47
【问题描述】:
我在 add_appointment.html 中创建了一个 html 表单,并在 MySQL 中创建了数据库“hospital”、表“appointment”。我可以在另一个名为 view_appointment.html 的 html 页面上查看我插入数据库的值,但我不知道如何从“add_appointment.html”文件中获取 html 表单输入并将其插入 MySQL 数据库。
我到处寻找解决方案,但我没有看到人们在 python Django 中这样做,它只是到处都是 php,而 python 但使用烧瓶,如果你能提供一些链接,我将不胜感激。
(我是一个初学者,在大学里,第一次做一个项目。直到现在我想通了,但被困在这里。我的项目几乎完成了,除了这件事,我保证如果有人帮助我,我成为很好,我也会尽力帮助别人)
filename: add_appointment.html
<!doctype html>
<html lang="en">
<head><title>Add Appointment</title></head>
<body>
<div class="container">
<form id="form" class="form">
<div class="from-control">
<h1> Add Appointment</h1>
<div class="form-control">
<label for="username">Name</label>
<input type="text" id="name" placeholder="Enter name">
<small>Error Message</small>
</div>
<div class="form-control">
<label for="email">Email</label>
<input type="text" id="email" placeholder="Enter email">
<small>Error Message</small>
</div>
<div class="form-control">
<label for="id">ID</label>
<input type="text" id="id" placeholder="Enter ID">
<small>Error Message</small>
</div>
<div class="form-control">
<label for="category">Disease</label>
<input type="text" id="category" placeholder=" disease">
<small>Error Message</small>
</div>
<button>Add</button>
</form>
</div>
<center><a href="view_appointment.html">View Appointments</a></center>
</body>
</html>
filename: appointment.py
import mysql.connector
import webbrowser
conn = mysql.connector.connect(user='root', password='Newtonlaw3', host='localhost', database='hospital')
if conn:
print("connected successfully")
else:
print("connection not established")
select_admin = """SELECT * FROM appointment"""
cursor = conn.cursor()
cursor.execute(select_admin)
result = cursor.fetchall()
p = []
tbl = "<tr><td><NAME</td><td>EMAIL</td><td>ID<td>SPECIALITY</td></tr>"
p.append(tbl)
for row in result:
a = "<tr><td>%s</td>" % row[0]
p.append(a)
b = "<td>%s</td>" % row[1]
p.append(b)
c = "<td>%d</td>" % row[2]
p.append(c)
d = "<td>%s</td></tr>" % row[3]
p.append(d)
contents = '''<!DOC TYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content ="text/html; charset=ISO-8859-1"
http-equiv = "content-type">
<title> View Appointments</title>
</head>
<body>
<table>
%s
</table>
</body>
</html>
''' % (p)
filename = 'view_appointment.html'
def main(contents, filename):
output = open(filename, 'w')
output.write(contents)
output.close()
main(contents, filename)
webbrowser.open(filename)
if (conn.is_connected()):
cursor.close()
conn.close()
print("my sql connection is closed")
【问题讨论】: