我正在尝试在HTML中创建一个按钮,单击该按钮后将转到下一页/上一页。我已经尝试过分页和小费,但似乎没有使它起作用。
这是我的应用程序:
from flask import Flask,render_template, request, json
from flask_mysqldb import MySQL
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'example'
app.config['MYSQL_USER'] = 'example'
app.config['MYSQL_PASSWORD'] = 'example'
app.config['MYSQL_DB'] = 'fund_raiser'
mysql = MySQL(app)
@app.route('/funds/pages/', defaults={'page':0})
@app.route('/funds/pages/<int:page>')
def fund_pages(page):
perpage = 5
first_page = page*perpage
cur = mysql.connection.cursor()
cur.execute("select * from fund_table limit "+str(first_page)+", "+str(perpage)+";", mysql.connection.commit())
data = cur.fetchall()
return render_template('funds.html', data=data)
我在HTML页面的哪里添加href标签?正确使用什么变量?
此外,我的代码是否容易受到此行中的SQL注入攻击,如果是,该如何解决:
cur.execute("select * from fund_table limit "+str(first_page)+", "+str(perpage)+";", mysql.connection.commit())
只要您只接受int作为用户输入,就可以大大降低SQL注入的风险-稍后将其转换为字符串不是问题。我已经将其称为安全的,因为如果在/ funds / pages /
之后收到不是int的内容,则路由将引发错误。但是,如果要使用准备好的语句(建议的“安全”方法),则应将执行更改为
cur.execute("select * from fund_table limit %s, %s;", params=(first_page, perpage), mysql.connection.commit())
单独发送查询参数不仅可以防止sql注入,而且还可以提高性能,因为查询的总体结构是预先定义的。您绝对应该在接受int以外的其他任何内容时立即使用它。
由于没有提供代码,因此无法真正回答您的第一个问题