siddaramesh siddaramesh - 29 days ago 13
MySQL Question

How to get MySQL database values in dropdown using python cgi?

I've created a webpage using Python cgi, now I want fill my HTML dropdown with database values. I don't know how get MySQL database values into dropdown.

#!/usr/bin/python
import MySQLdb
import cgi

import cgitb; cgitb.enable()

print "content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<meta charset="utf-8">'
print '<meta http-equiv="X-UA-Compatible" content="IE=edge">'
print '<meta name="viewport" content="width=device-width, initial-scale=1">'
print '<link rel="stylesheet" href="/powersupply/cssmenu/styles.css">'
print '<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>'
print '<script src="/powersupply/cssmenu/script.js"></script>'
print '<title>Power Supply Testing</title>'
print '<link rel="stylesheet" href="css/newstyle.css">'
print '</head>'
print '<body>'

print '<form name="myform" method="post" action="">'
print '<div>'
print '<fieldset class="fieldset">'
print '<legend>Power Supply Testing</legend>'
print '<table>'
print '<tr>'
print '<td>'
print '<label class="label">Serial Num Option:</label>'
print '</td>'
print '<td>'
print '<select class="textbox">'
print '<option value="serialno">Serial Num Auto Generate</option>'
print '<option value="barcode">Barcode Scanner Capture</option>'
print '</select>'
print '</td>'
print '<td>'
print '<label class="label">Serial Num:</label>'
print '</td>'
print '<td>'
print '<input class="textbox" type="text" name="serial" id="serial"/>'
print '</td>'
print '<td>'
print '<label class="label">PartNum:</label>'
print '</td>'
print '<td>'
print '<select class="textbox">'
print '<option value=""></option>'
print '</select>'
print '</td>'
print '</tr>'
print '<tr>'
print '<td>'
print '<label class="label">Revision No:</label>'
print '</td>'
print '<td>'
print '<select class="textbox">'
print '<option value=""></option>'
print '</select>'
print '</td>'
print '<td>'
print '<label class="label">Description:</label>'
print '</td>'
print '<td>'
print '<input class="textbox" type="text" name="description" id="description"/>'
print '</td>'
print '</tr>'
print '</table>'
print '<table>'
print '<tr>'
print '<td>'
print '<input class="clicktosave" type="submit" value="CLICK TO RECORD"/>'
print '</td>'
print '</tr>'
print '<tr>'
print '<td>'
print '<input class="cancelbutton" type="reset" name="cancel" value="CANCEL" id="cancel"/>'
print '</td>'
print '</tr>'
print '</table>'
print '</fieldset>'
print '</div>'
print '</form>'


print '</body>'
print '</html>'


I want to add MySQL database values in the dropdown. in between Python code PHP script is not working. Is there any way to get database values?

Answer

You are going to want to use something like this.

Here is an example of how to use this package:

import MySQLdb

def db_dropdown():  # Execute query
    db = MySQLdb.connect(user='root', db='test', passwd='test', host='ip-address')  # Your DB details here
    cursor = db.cursor()

    sql = 'SELECT SUM(regressnum) FROM testlog WHERE buildnumber = %s AND productid = 41))' \
          % build_number  # Parse in a variable into the query
    cursor.execute(sql)
    list_tested = cursor.fetchall()  # Get query response and store in variable
    list_tested = [i for sub in list_tested for i in sub]  # Convert to list from tuple

    return list_tested

def print_dropdown(data):  # Print the dropdown
    print '<div>'
    print '<select>'
    for i in data:
        print '<option value="%s"selected>%s</option>' % (i, i)
    print '</select>'
    print '</div>'

Example usage:

print_dropdown(db_dropdown())

Obviously this can be expanded upon and can be made to query anytihng and produce anything.

Comments