Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add order by from user input? #1640

Closed
sytranvn opened this issue May 1, 2024 · 1 comment
Closed

How to add order by from user input? #1640

sytranvn opened this issue May 1, 2024 · 1 comment

Comments

@sytranvn
Copy link

sytranvn commented May 1, 2024

How can I do this?

request.input("order_by", ?, "id")
request.input("order", ?, "asc")
        SELECT 
          *
        FROM table
        ORDER BY @order_by @order
@dhensby
Copy link
Collaborator

dhensby commented May 2, 2024

It's not possible to pass identifiers (ie: column names, table names, etc) as parameters in SQL so this isn't possible as you've shown.

To do something like that you'll need a whitelist of allowed columns / orders and construct the query off the back of that, rather than allowing users to provide raw column names, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants