3

Code -

$price = mysqli_real_escape_string($connect,trim($results['price']));

price is retrieved from the database, and then echoed using -

echo $price; 

Question - Is this safe enough from XSS or SQL Injection? It simply includes numbers.

Thanks

3
  • Is $results['price'] user input? Commented Feb 28, 2011 at 21:37
  • @CJD no. It is mysqli_fetch_array Commented Feb 28, 2011 at 21:38
  • SQL injection can only occur if user actions write to the database. Commented Feb 28, 2011 at 21:38

5 Answers 5

4

The checking should probably be done during data input, but you could be safe and also check at output. I would just use is_numeric or something similar to ensure that the output is, indeed, a number.

Sign up to request clarification or add additional context in comments.

Comments

0

Standard practice is to use htmlspecialchars() or htmlentities() to escape output to the browser for protection against XSS. However as mentioned in another answer, the real issue is whether correct data was stored in the database in the first place. It should be properly validated and escaped before storage in the database.

Comments

0

To be safe you need to asses if the input for $results['price'] from user input.

If it is you need to strongly validate/sanitize it as an expected value before sending it to SQL.

Comments

0

If your data is only read from the db you can ignore using mysqli_real_escape_string(). It's valuable for escaping user input which should be written to the db.

Comments

0

mysqli_real_escape_string() is used to escape data about to be inserted IN the database, to prevent SQL Injection. It has nothing to do with XSS.

You have to use htmlentities() to do what you want to do. I suggest you to read eykanal answer and use is_numeric to validate.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.