The code confirms the HTML template that it has deleted the row of the entered pincode from the student database list of accounts but on checking the list it has NOT deleted that row of the entered pincode, it is still seen in the list, where could I possibly have gone wrong? I have provided everything below that may help to see what I missed that does NOT delete the particular row.
Below is the app.py
from flask import Flask, render_template, jsonify, request, url_for
import sqlite3
app = Flask(__name__)
#Delete Page
@app.route("/studentdeletepage")
def student_deletepage():
return render_template("student_deletepage.html")
#Deleted Action on the Account Page
@app.route("/studentdeleted",methods=["POST"])
def student_deleted():
#Grab the pincode of the student account and delete the student account
#CCEC
pincode = request.form["pin"]
# Connect
with sqlite3.connect("student.db") as conn:
try:
#Cursor
cur = conn.cursor()
#Execute
cur.execute("DELETE FROM students WHERE pincode=(?)", (pincode))
#Commit
#conn.commit()
except:
#conn.rollback()
return f"Cannot be Deleted."
finally:
msg = f"Your Account with Pincode {pincode}, has been deleted successfully. "
return render_template("student_delete_finalpage.html", deleted_msg=msg)
if __name__== "__main__":
app.run(debug=True)
The student_deletepage.HTML is seen as below
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Deleted Records</title>
</head>
<body><form action="/studentdeleted" method="post" >
<h2>Student Delete Record Page</h2>
<h3>Please Enter Your Passcode to delete your account.</h3>
Pincode<br><input type="text" name="pin"/><br>
<tr><td><input type="submit" value="Delete"/></td></tr>
</form>
<h3><a href="/">Click Here Back to Home Page</a> </h3>
<h3><a href="/newstudent">Register </a></h3>
<h3><a href="/studentpage">Back to Swissport Student Page</a></h3>
<h3><a href="/studentlist">Registered Student List Page </a></h3>
</body>
</html>
The student_delete_final page.HTML is seen as below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Deleted Records</title>
</head>
<body><h1>{{deleted_msg}}</h1>
<h3><a href="/">Click Here Back to Home Page</a> </h3>
<h3><a href="/newstudent">Back to the Sign Up Page</a></h3>
<h3><a href="/studentpage">Back to Swissport Student Page</a></h3>
<h3><a href="/studentlist">Back to Student List Page </a></h3>
<h3><a href="/studentdeletepage">Delete Your Account Here </a></h3>
</body>
</html>
This is the student_list.HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Student List 2022 Records</title>
</head>
<body>
<table border=1>
<thead>
<td>Name</td>
<td>Address</td>
<td>Gender</td>
<td>City</td>
<td>Pincode</td>
</thead>
{% for row in rows %}
<tr>
<td>{{row["name"]}}</td>
<td>{{row["addr"]}}</td>
<td>{{row["gender"]}}</td>
<td>{{row["city"]}}</td>
<td>{{row["pin"]}}</td>
</tr>
{% endfor %}
</table>
<h3><a href="/">Click Here Back to Home Page</a> </h3>
<h3><a href="/newstudent">Register </a></h3>
<h3><a href="/studentpage">Back to Swissport Student Page</a></h3>
<h3><a href="/studentlist">Registered Student List Page </a></h3>
<h3><a href="/studentdeletepage">Delete Your Account Here </a></h3>
</body>
</html>
And finally below is new_student.HTML where pincode originates from:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Student Profile Page</title>
</head>
<body><form method="POST" action = "/studentrecord" >
<h3>Student Information Page</h3>
Name<br><input type="text" name="nm"/><br>
Address<br><textarea name="add"></textarea><br>
Gender<br><input type="text" name="gd"/><br>
City<br><input type="text" name="city"/><br>
Pincode<br><input type="text" name="pin"/><br>
<tr><td><input type="submit" value="Submit"/></td></tr>
</form>
<h3><a href="/">Click Here Back to Home Page</a> </h3>
<h3><a href="/newstudent">Register </a></h3>
<h3><a href="/studentpage">Back to Swissport Student Page</a></h3>
<h3><a href="/studentlist">Registered Student List Page </a></h3>
</body>
</html>
And below is the creation of database studentdb.py
import sqlite3
conn = sqlite3.connect("student.db")
print("Student Database Opened Successfully.")
#Create Table
conn.execute("create table students(name TEXT NOT NULL, addr TEXT NOT NULL, gender TEXT NOT NULL, city TEXT NOT NULL, pin TEXT UNIQUE NOT NULL)")
print("Student Table created Successfully.")
conn.close()