0

I'm using mySqli and I'm trying to develop the ultimate sanitizing function before append/update the database.

  1. I was wondering if that super simple function can do the job for me.

    function sanitize($me) {
    return mysql_real_escape_string($me);
    }
    
  2. Is internal PHP command like date and time (for example sanitize(date("F j, Y, g:i a")); should also be sanitized ?

2 Answers 2

5

if that super simple function can do the job for me.

No. Just because underlying function has absolutely nothing to do with sanitization, even from the proper extension.

Is internal PHP command output should also be sanitized ?

Yes. Because your db-related logic should be ignorant of the data source. All it should know that this data is coming into query and thus have to be properly formatted.

Prepared statements is the only way to reach the goal. So, you have to get rid of this function and start learning prepared statements

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

Comments

2

Everything should be sanitized, no matter if it is user input, result from function call or even hard-coded data. One day you will decide to make the date format dynamic, allowing user to set the format string, and you'll forget to sanitize it; the user will add quotes to the format and your SQL will be broken.

mysql_real_escape_string do not belong to the mysqli extension, use the correct function. Even better, use PDO and you don't need a function, only param binding.

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.