MYSql, CGI web page search code not working

Fred fredg at fastmail.fm
Sat Jan 28 16:42:58 CET 2006


OK,

I can now successfully enter data into my MySQL database through my CGI
web page. I can click a button and retrieve all the records, but I can
not seem to get the search code to work.

Below is the web page code and then my Python script. When I click my
search button it just gives me all the records

I know this line is executing: cursor.execute("Select * from phone
where name = name order by name")

Because I played with the "order by" but it seems to ignore my where
clause.

No matter what I type in the form text box (or even if I leave it
blank) I get all the records.

I can hard code this line to: cursor.execute("Select * from phone where
name = 'Fred' order by name")

and it returns the one record corectly.

Any ideas?

Fred

------------------------------------------
<form action="cgi-bin/searchdata.py">
 <p>Enter the name to find:
<p><input type="text" name="name" size="30">
<input type="submit" value="Search">
</form>
------------------------------------------

#!/usr/local/bin/python
print "Content-Type: text/html\n"
import MySQLdb
import cgi

db=MySQLdb.connect(host = 'localhost', db = 'phone')
cursor=db.cursor()
cursor.execute("Select * from phone where name = name order by name")

result = cursor.fetchall()
for record in result:
   print '<p>'
   print record[0]
   print '--'
   print record[1]
   print '--'
   print record[2]
   print '--'
   print record[3]
   print '</p>'




More information about the Python-list mailing list