开发者

double loop in psp/python/html from a mysql query

I'm trying to program a script that will take in a user input of a place they want to go, starting with the country. Then take the user's input and update my list of which state is in the country then, which city is in the state AND country.

I'm using python/psp for my backend and html for my front end. I'm having trouble getting this page update to work on my site. Can anyone help?

<div id="content">


<p>
Where would you like to go?
</p>
<form method="post" action="http://localhost/list2.psp">
Places to go:
<select name = "location">
<%
cursor.execute("select distint country from lo开发者_高级运维cation") or die(mysql_error())
result = cursor.fetchall()
for record in result:
cursor.execute("select * from location where country='%s'" % record[0]) or die(mysql_error())
result2 =  cursor.fetchall()
%>

<optgroup label="<%=record[0]%>
<%

for record2 in result2:
%>

<option value="<%= record2[0] %>"><%= record[1] %>, <%= record[2]%>, <%= record[3]%></option>

<%
print 'hi'
%>

</optgroup>
</select>
<br>


<input type="submit" value="Search">`
</form>
</div>

it gives me this error on my webpage:

cursor.execute("select * from location where country='%s'" % record[0]) or die(mysql_error())

         ^

IndentationError: expected an indented block

is my syntax wrong?


I have never heard of Python Server Pages, wow. Anyway, in Python indentation matters, you can think of indentation as the replacement for curly braces in C-style languages.

for record in result:
    cursor.execute("select * from location where country='%s'" % record[0]) or die(mysql_error())
    result2 =  cursor.fetchall()
    for record2 in result2:
          pass #next loop, further indented

So in your for loop, everything that belongs to the loop, has to be indented on the same level.


To see how the python is being translated follow the "Debugging" section in this O'Reilly tutorial: http://www.oreillynet.com/pub/a/python/2004/02/26/python_server_pages.html

For fixing the python PSP code, see this link http://www.webwareforpython.org/Webware/PSP/Docs/UsersGuide.html#CodeThatSpansTags or try using braces: http://www.webwareforpython.org/Webware/PSP/Docs/UsersGuide.html#Braces

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜