0

I'm receiving from a JS script an string that should be formatted according to the datetime format so like: 2013-07-29 00:00:00.

Of course I need to sanitize that input before putting it in a raw MySQL query. What will be the best way to sanitize that format on php?

DomingoSL
  • 14,920
  • 24
  • 99
  • 173

4 Answers4

3

The "best" would be to use prepared statements and let the DB adapter escape special chars in the values for you.

Whatever is your flavor does not matter really in that subject:

As suggested by knittl, see How can I prevent SQL injection in PHP? for the canonical answer.

Community
  • 1
  • 1
Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125
2

The BEST would to use PDO.

Otherwise if you are using the deprecated mysql_* functions; mysql_real_escape_string() is what you are looking for.

Eric
  • 18,532
  • 2
  • 34
  • 39
1

You cand use a regular expression for testing the input format.

For inserting into DB best would be PDO.

1

I would personally make a Regular Expression, take apart each date and time component, and verify that it is actually a good date and time in PHP (not like 2013-25-19 12:72:883). You can use checkdate() to verify that it is actually a date, then check the time based on the expected hour, minute, and second range for time..

Also, it is common to use the PDO extension in PHP to insert into the database.

BLaZuRE
  • 2,356
  • 2
  • 26
  • 43