python - 来自 mysql 查询的 psp/python/html 中的双循环

标签 python html mysql-python

我正在尝试编写一个脚本,让用户输入他们想去的地方,从国家开始。然后获取用户的输入并更新我的列表,其中列出了哪个州在该国家/地区,哪个城市在州和国家/地区。

我的后端使用 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/

相关文章:

python - 在不使用库的情况下以格式打印表格,SQLite 3 python

python - 是否有任何在设计时考虑到人类的 Python XML 解析器?

html - 使用 CSS 保持 div 的纵横比

javascript - 如何停止 dragend 在拖放中的默认行为?

python - 如何将 xampp 中的 MYSql 用于 python 3?

python - pip 安装 MySQL-python 时出现错误代码 1

python - (psycopg2.OperationalError) 无效 - 操作码

子域的 HTML5 localStorage 大小限制

mysql - 找不到 django/mysql 表 - django.db.utils.ProgrammingError : (1146, "Table ' truSTLine.authentication_user' 不存在”)

python - GeoDataFrame.plot 有效但不显示在 folium GeoJson 中