Database Reference
In-Depth Information
image_file = form [ 'upload_file' ]
else :
image_file = None
According to most of the documentation that I have read, the file attribute of an object
that corresponds to a file field should be true if a file has been uploaded. Unfortunately,
the file attribute seems to be true even when the user submits the form but leaves the
file field blank. It may even be the case that the type attribute is set when no file actually
was uploaded (for example, to application/octet-stream ). In my experience, a more
reliable way to determine whether a file was uploaded is to test the filename attribute:
form = cgi . FieldStorage ()
if form . has_key ( 'upload_file' ) and form [ 'upload_file' ] . filename :
print ( "<p>A file was uploaded</p>" )
else :
print ( "<p>A file was not uploaded</p>" )
Assuming that a file was uploaded, access the parameter's value attribute to read the
file and obtain its contents:
data = form [ 'upload_file' ] . value
See the post_image.py script for details about how to use this function to get image
information and store it in MySQL.
20.9. Performing Web-Based Database Searches
Problem
You want to implement a web-based search interface.
Solution
Present a form containing fields that enable the user to supply search parameters such
as keywords. Use the submitted keywords to construct a database query, then display
the query results.
Discussion
A script that implements a web-based search interface provides a convenience for people
who visit your website because they need not know any SQL to find information in your
database. Instead, visitors supply keywords that describe what they're interested in and
your script figures out the appropriate statements to run on their behalf. A common
paradigm for this activity involves a form containing one or more fields for entering
search parameters. The user fills in the form, submits it, and receives back a new page
containing the records that match the parameters.
As the writer of such a script, you must handle these operations:
Search WWH ::




Custom Search