If mktime fails to create a timestamp from (Free web servers)
If mktime fails to create a timestamp from the date you passed to it, it will return -1. This happens when the input is invalid, although it matches the regular expression. For example, 99-99-9999 will pass the regular expression test but is obviously not a valid date. To be sure that the date is indeed a date, you test for the return value from mktime and respond accordingly. if ($movie_release == -1 ) { $error .= Please+enter+a+real+date+ . with+the+dd-mm-yyyy+format%21%0D%0A ; } In this case, a false date entry triggers an error message asking for a valid date. Here s an alternative technique: You could have performed the same timestamp generation using SQL. Many things that PHP does on the string manipulation side can be done straight from SQL, as shown here: if (!ereg( ([0-9]{2})-([0-9]{2})-([0-9]{4}) , $movie_release, $reldatepart) || empty($movie_release)) { … } $reldate = $reldatepart[ 3 ] . - . $reldatepart[ 2 ] . - . $reldatepart[ 1 ] . 00:00:00 ; $sql = INSERT INTO movie (movie_release) . VALUES (UNIX_TIMESTAMP( $reldate )) ; In this code, the SQL does the timestamp generation. The UNIX_TIMESTAMP() SQL function expects a YYYY-MM-DD HH:MM:SS (2004-12-05 02:05:00) format and creates a timestamp from it. In the code, you force the creation of the timestamp at 00:00 on the date of the movie release. You can save yourself some lengthy coding by using SQL features wherever possible. See documentation on MySQL date and time functions at www.mysql.com/doc/en/ Date_and_time_functions.html. Summary Validating user data is all about being prepared for the worst. Users make mistakes that s the nature of users. Most errors are unintentional, but some are made intentionally to deny the service. It happens every day. The developer has to help the system deal with user input errors. Regular expressions help you meet many user input validation challenges. Learning how to use them is often the key to success in an interactive system. Exercise 1. Add validation to make the lead actor and director selections required. 250 Chapter 8
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.