MYSql, CGI web page search code not working

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


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

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?


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

print "Content-Type: text/html\n"
import MySQLdb
import cgi

db=MySQLdb.connect(host = 'localhost', db = 'phone')
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