943,822 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 3205
  • PHP RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Nov 2nd, 2008
1

Re: Validate Input Fields

Click to Expand / Collapse  Quote originally posted by antwan1986 ...
Perhaps I've misunderstood the question, and the two previous posters used regular expressions but I would normally test a date with something like the following:

PHP Syntax (Toggle Plain Text)
  1. function is_date($value) {
  2. // If the value of the field is empty, immediately return false.
  3. if (empty($value)) { return FALSE; }
  4.  
  5. // Attach the exploded values to variables.
  6. list($day, $month, $year) = explode("/", $value);
  7.  
  8. // Check those variables in the checkdate function. If it's valid, the function returns true.
  9. return checkdate($month, $day, $year);
  10. }
This is a great method for the date. If you wanted to you could even return "$year - $month - $day"; if it is a valid date or FALSE if it isn't seeing as you already have the variables set.

As for the time, I would probably take the input as a string and use one of the regular expression checks above to see if it consists of 4 numbers, then typecast it to an integer and check if ($time_num >= 900 && $time_num <= 1700) { echo "time is valid"; }
Reputation Points: 20
Solved Threads: 13
Junior Poster in Training
humbug is offline Offline
93 posts
since Oct 2005
Nov 5th, 2008
0

Re: Validate Input Fields

Great Stuff!

Your postings have giving me much understanding of most issues.

start_time is a varchar data type, is it possible to perform an addition on this variable?

Supposing $start_time has a value 1100 because I am using 24 hour format can I add a value to it?

ie. $start_time = $start_time + 100;
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jackakos is offline Offline
50 posts
since Sep 2008
Nov 6th, 2008
0

Re: Validate Input Fields

In php, if you try to perform an operation on $start_time (assuming that it is a variable in the script containing a string) it will be used as an integer. It works fine if the string is something like "1234". $start_time will then be converted to an integer variable (because it is on the left of the "=").

If there is a column in a mysql table that is named "start_time" and is a varchar datatype and you do something like $start_time = $row['start_time'] (where $row contains a row of a result from a query) then $start_time will be a string.

If you are writing a query to submit data to the mysql database and you can do something like
php Syntax (Toggle Plain Text)
  1. $query = "INSERT INTO table (`start_time`, `finish_time`) VALUES ( " . $start_time . ", " . $finish_time . ")";
  2. //or
  3. $query = "INSERT INTO table (`start_time`, `finish_time`) VALUES ( '" . $start_time . "', '" . $finish_time . "')";
With the single quotes ('), the mysql treats whatever is in them as a string no matter what. When you do $string = "$int"; or $string = "number is " . $int; , $int is used as a string. So when you are creating the query it doesn't matter what type $start_time is. Without the single quotes I think SQL will assume it's whatever type it needs to be. (mysql converts it to the right data type anyway so there's usually no problem). Just be careful doing "... WHERE culumn_name=$string" instead of "... WHERE culumn_name='$string'" because other wise only the first word of $string will be used.

This changing types is called "typecasting" if you want to research it more or anything like that.
Reputation Points: 20
Solved Threads: 13
Junior Poster in Training
humbug is offline Offline
93 posts
since Oct 2005
Nov 6th, 2008
0

Re: Validate Input Fields

Great stuff and thanks to everyone who shared an idea.

Thanx
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jackakos is offline Offline
50 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: need help with two simple php scripts
Next Thread in PHP Forum Timeline: php validation before file upload





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC