我正在尝试编写一个脚本,让用户输入他们想去的地方,从国家开始。然后获取用户的输入并更新我的列表,其中列出了哪个州在该国家/地区,哪个城市在州和国家/地区。
我的后端使用 python/psp,前端使用 html。我无法让此页面更新在我的网站上运行。谁能帮忙?
<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 location") 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>
它在我的网页上给我这个错误:
cursor.execute("select * from location where country='%s'" % record[0]) or die(mysql_error())
^
IndentationError: expected an indented block
我的语法错了吗?
最佳答案
我从来没有听说过 Python 服务器页面,哇。无论如何,在Python indentation matters ,您可以将缩进视为 C 风格语言中花括号的替代品。
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
因此在您的 for 循环中,属于循环的所有内容都必须在同一级别缩进。
关于python - 来自 mysql 查询的 psp/python/html 中的双循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7157550/