How correct validate data from user to save in database from inputs?

I use this for textfields:

        $name = trim(sanitize_text_field($_POST['name']));

But if i add special signs like "&*({} Something like this ^&({}<>", then they add without a problem.

I need add only "Something like this" without special signs.


Try $name = trim( sanitize_user( $_POST['name'], true ) );

Be sure that the function does what you want! Read here

  • Work, thanks. I have question about secure data. How check if variable is INT? – polyman Apr 22 at 17:30
  • 1
    There are some... like "is_int" to verify if it's exact an int. But if you want to see if a string can be converted to int, the "is_numeric" is a function that can check that. If you want to parse a string to int/float you can use "intval" or "floatval". And be sure that the expected int is a valid one. If you expect an int that is a positive number, then be sure is it, or else you could have some bugs. – Brada Apr 23 at 9:42
  • 1
    Be careful if you use "is_numeric", as will validate hex strings as integers, and mysql will see them as strings, example here – Brada Apr 23 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.