P粉1419251812023-07-25 18:20:58
Here's how you might handle this in your code:
if (Number.isInteger(data.topUsersCount)) { let queryString = "SELECT userName, clicks FROM users ORDER BY clicks DESC LIMIT " + data.topUsersCount; con.query(queryString, (err, topUsersData) => { // Handle data }); } else { // Handle error console.log("Invalid limit value"); }
In the above code, use Number.isInteger(data.topUsersCount) to check whether data.topUsersCount is a safe integer before incorporating it into the query string. This is critical to preventing SQL injection attacks.