Flask pagination iter pages preveneting SQL injection and adding nextpage button in HTML temp在Flask pagination iter pages中预先插入SQL注入并添加nextpage按钮

2024-09-29 08:26:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在我的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())

Tags: fromconfigappexamplemysqlpagepagesconnection
1条回答
网友
1楼 · 发布于 2024-09-29 08:26:16

只要您只接受int作为用户输入,SQL注入的风险就会大大降低—以后将其转换为字符串不是问题。我已经称之为safe了,因为如果在/funds/pages/

但是,如果要使用准备好的语句(推荐的“secure”方法),应该将执行更改为

cur.execute("select * from fund_table limit %s, %s;", params=(first_page, perpage), mysql.connection.commit())

单独发送查询参数不仅可以防止sql注入,而且还可以提高性能,因为查询的整体结构是预定义的。当您接受除int以外的任何内容进行分页时,一定要使用这个。你知道吗

无法真正回答您的第一个问题,因为没有提供代码

相关问题 更多 >