I have created an html form in add_appointment.html and have created database "hospital", table "appointment" in MySQL. I able to view values that I inserted into my database on another html page called view_appointment.html but I do not know how to take html form input from the "add_appointment.html" file and insert it into MySQL database.
I searched for a solution everywhere, but I don't see people doing it in python Django, its just php everywhere and python but with flask, if you can help with some link I'll be grateful.
(I'm a beginner, in college, doing a project for the first time. I figured it out somehow until now but got stuck here. My project is almost done except this one thing, I promise if someone helps me the day I become good enough I'll try my best to help others as well )
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")