MYSql,CGI网页搜索代码无效 [英] MYSql, CGI web page search code not working
问题描述
好的,
我现在可以通过我的CGI
网页成功将数据输入我的MySQL数据库。我可以单击一个按钮并检索所有记录,但我可以
似乎没有让搜索代码起作用。
下面是网页代码然后我的Python脚本。当我点击我的
搜索按钮时它只给我所有记录
我知道这行正在执行:cursor.execute(" Select * from phone
其中name = name name by name")
因为我使用了order by但似乎忽略了我在哪里
条款。
无论我在表格文本框中输入什么(或者即使我留下它
空白)我得到了所有的记录。
我可以硬编码这行:cursor.execute(" Select * from phone where
name =''Fred''按名称排序")
并且它返回核心的一条记录。
任何想法?
弗雷德
--------------------------- ---------------
< form action =" cgi-bin / searchdata.py">
< p>输入要查找的名称:
< p>< input type =" text"命名= QUOT;名称" size =" 30">
< input type =" submit" value =" Search">
< / form>
--------------------- ---------------------
#!/ usr / local / bin / python
打印Content-Type:text / html\\\
导入MySQLdb
导入cgi
db = MySQLdb .connect(host =''localhost'',db =''phone'')
cursor = db.cursor()
cursor.execute(" Select *来自手机的名称=名称按名称排序")
result = cursor.fetchall()
记录结果:
打印''< p>''
打印记录[0]
打印'' - ''
打印记录[1 ]
打印'' - ''
打印记录[2]
打印'' - ''
打印记录[3]
打印''< / p>''
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>''
推荐答案
>
db = MySQLdb.connect(host =''localhost'',db =''phone' ')
cursor = db.cursor()
cursor.execute(" select * from phone where name = name order by name")
db=MySQLdb.connect(host = ''localhost'', db = ''phone'')
cursor=db.cursor()
cursor.execute("Select * from phone where name = name order by name")
你没有参数化查询。因此where子句是重言式,
因为名字总是这个名字。
做这样的事情:
cursor.execute(" Select * from phone where name =?order by name",(name,))
实际上可能需要使用与?不同的东西? to
指定参数 - 这取决于你的DB-Api的参数样式。
在口译员中检查
导入MySQLdb
打印mySQLdb.paramstyle
Diez
You don''t parametrize the query. The where-clause thus is a tautology,
as the name is always the name.
Do something like this:
cursor.execute("Select * from phone where name = ? order by name", (name,))
Actually it might be necessary to use something different from the ? to
specify the parameter - that depends on the paramstyle of your DB-Api.
Check that in the interpreter with
import MySQLdb
print mySQLdb.paramstyle
Diez
print MySQLdb.paramstyle返回:format
我找到了一个这样的例子:
cursor.execute(''''''从电话中选择*,其中name =%s顺序通过
名称'''''',(名称))
但我在我的Apache错误日志中得到这个:
NameError:名称''name''未定义
就像我上次发布的问题一样,我确信它非常简单
我是失踪!!
Fred
print MySQLdb.paramstyle returns: format
I found one example like this:
cursor.execute(''''''Select * from phone where name=%s order by
name'''''',(name))
But I get this in my Apache error log:
NameError: name ''name'' is not defined
Like my last problem I posted, I am sure it is something very simple
that I am missing!!
Fred
Fred写道:
无论我在表格中输入什么文本框(或者即使我留下它
空白)我得到所有记录。
No matter what I type in the form text box (or even if I leave it
blank) I get all the records.
试试这个:
#!/ usr / local / bin / py thon
print" Content-Type:text / html\\\
"
import MySQLdb
import cgi
>
db = MySQLdb.connect(host =''localhost'',db =''phone'')
cursor = db.cursor()
cursor.execute(" Select * from phone where name =%s order by name name,,(name,))
result = cursor.fetchall()
$ b结果记录$ b:
打印''< p>''
打印记录[0]
打印'' - ''
打印记录[1]
打印'' - ''
打印记录[2]
打印'' - ''
打印记录[3]
打印''< / p>''
(假设您的文本字段的名称是name。)
Try this:
#!/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=%s order by name", (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>''
(Assuming the name of your text field is "name".)
这篇关于MYSql,CGI网页搜索代码无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!