I have created a CGI script using python 3.6 wherein it will take user input from a HTML form and then search the database based on the value.
How should I take the input and filter data based on the value? My current script is showing the entire table instead of filtering.
Basically output needs to be based on "Where" condition.
HTML Script :
<!DOCTYPE html>
<html>
<body>
<h1>Sender ID Blacklist</h1>
<p>Please enter Sender id below</p>
<form method='get' action='http://localhost/cgi-bin/test3.cgi'>
<p>Senderid: <input type='text' Senderid='send' /></p>
<input type='submit' value='Submit' />
</form>
</body>
</html>
Below is the CGI script (test3.cgi) :
import mysql.connector as mx
import sys
import cgi
import cgitb
def htmltop():
print("""Content-type: text/html\r\n\r\n
<!DOCTYPE html>
<html>
<body>Output:""")
def htmltail():
print("""</body></html>""")
def connectdb():
mydb=mx.connect(host='127.0.0.1',
user='root',
passwd='',
port='3306',
database='arghad')
cur=mydb.cursor()
return mydb,cur
def Blacklist(mydb,cur):
query="select * from arghad.test"
cur.execute(query)
result=cur.fetchall()
return result
def displaytest(result):
print("<table border='5'>")
print("<tr>")
print("<th>senderid</th>")
print("<th>route</th>")
print("<th>time</th>")
print("</tr>")
for x in result:
print("<tr>")
print("<td>{0}</td>".format(x[0]))
print("<td>{0}</td>".format(x[1]))
print("<td>{0}</td>".format(x[2]))
print("</tr>")
print("</table>")
if __name__ == '__main__':
try:
htmltop()
form = cgi.FieldStorage()
mydb,cur=connectdb()
result=Blacklist(mydb,cur)
displaytest(result)
cur.close()
htmltail()
except:
cgi.print_exception()
Output :
I have given only one value by the way.