Error Checking Code

When I type in a date for the fields "Submitted to Office" or "Submitted to Supervisor" in the format 2011-05-05 & then click the submit button, it gets entered into my MySQL databse correctly.

My question is, is there any way to edit this code so the user will get a pop up box if the enter a date other than in the format YYYY-MM-DD?

If I can't use jscript to do this, how do I add this in the PHP error checking part of the code?
<b> <p> <i> To go to the main page <a href="http://tnep-g-psrflow/flow/index.html">click here</a>.</p> </i><?php # edit_dqa.php$page_title = 'Edit a Record';$con = mysql_connect("localhost","username","password");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("psrflow", $con);ini_set('display_errors',1); error_reporting(E_ALL);if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { 	$id = $_GET['id'];} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { 	$id = $_POST['id'];} else { // No valid ID, kill the script.	echo '<h1 id="mainhead">Page Error</h1>	<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';	include ('./includes/footer.html'); 	exit();}if (isset($_POST['submitted'])) {	$errors = array(); 	if (empty($_POST['pacts'])) {		$errors[] = 'You forgot to enter your PACTS number. If there is no PACTS # you can enter 0000';	} else {		$pt = $_POST['pacts'];	}		if (empty($_POST['fname'])) {		$errors[] = 'You forgot to enter the first name.';	} else {		$fn = $_POST['fname'];	}		if (empty($_POST['lname'])) {		$errors[] = 'You forgot to enter the last name.';	} else {		$ln = $_POST['lname'];	}		if (empty($_POST['date_sub_crt'])) {		$errors[] = 'You forgot to enter a date submitted to office!.';	} else {		$crt = $_POST['date_sub_crt'];	}			if (empty($_POST['date_disclo_att'])) {		$errors[] = 'You forgot to enter the supervisor disclosure date!';	} else {		$disc = $_POST['date_disclo_att'];	}			if (empty($errors)) { 			$query = "UPDATE psrinfo SET pacts='$pt', fname='$fn', lname='$ln', date_sub_crt='$crt', date_disclo_att='$disc' WHERE fid=$id";	$result = @mysql_query ($query); // Run the query.		$EmailAddress = 'email@yahoo.com';	$Message = "A clerk has entered a 'Date Submitted to supervisor or office'. Please enter/edit this information in PACTS.\n \n PACTS No : ${_REQUEST['pacts']}\n First Name : ${_REQUEST['fname']} \n Last Name : ${_REQUEST['lname']} \n Date Submitted to office : ${_REQUEST['date_sub_crt']} \n Date Dislosed to supervisor : ${_REQUEST['date_disclo_att']} ";	$Subject  = "A Clerk has Edited a Record.";$Headers = "From: PODDS@yahoo.com \n"; if (mail($EmailAddress, $Subject, $Message, $Headers)) {				echo "<p><center><h2>My Office</h2></center></p>";echo "<p><h3><center>Eastern District</center></h3></p>";echo "<center>An email has been sent to your supervisor for approval and ";  } else {  echo "This system is not working properly. Please contact IT so they can fix it.";  } 				$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);if ((substr($url, -1) == '/') OR (substr($url, -1) == '//')) {$url = substr ($url, 0, -1);}$url ='/flow/index.html'; header("Location: $url");exit();			} else { // Report the errors.			echo '<h1 id="mainhead">Error!</h1>		<p class="error">The following error(s) occurred:<br />';		foreach ($errors as $msg) { // Print each error.			echo " - $msg<br />\n";		}		echo '</p><p>Please try again.</p><p><br /></p>';			} } $query = "SELECT pacts, fname, lname, date_sub_crt, date_disclo_att, date_disclo_att FROM psrinfo WHERE fid = " . $_REQUEST['id'];$result = @mysql_query ($query); // Run the query.if (mysql_num_rows($result) == 1) { 	$row = mysql_fetch_array ($result, MYSQL_NUM);		?>	<?phpecho '<form action="clerk_edit.php" method="post"><fieldset><legend><h2> You are editing a record! After submitting this form, your DQA will be notified of the change via email.</h2></legend><b>PACTS No:</b> <br><input type="text" name="pacts" size="15" maxlength="30" value="'.$row[0].'" /><br><b>First Name:</b> <br><input type="text" name="fname" size="15" maxlength="30" value="'.$row[1].'" /><br /><b>Last Name:</b> <br><input type="text" name="lname" size="15" maxlength="30" value="'.$row[2].'" /><br /><b>Submitted to office: </b><br><input type="text" name="date_sub_crt" size="15" maxlength="30" value="'.$row[3].'" /> <br><b>Submitted to supervisor: </b><br><input type="text" name="date_disclo_att" size="15" maxlength="30" value="'.$row[4].'" /> <br></fieldset><div align="left"><input type="submit" name="submit" value="Submit" /></div> <input type="hidden" name="submitted" value="TRUE" /><input type="hidden" name="id" value="' . $id . '" /></form>';} else { // Not a valid user ID.	echo '<h1 id="mainhead">Page Error</h1>	<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';}mysql_close(); // Close the database connection.?>

Open in new window

LVL 15
wantabe2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Cornelia YoderArtistCommented:
$date = '2011-03-04';

$array = explode('-', $date);

if ($array[0]<2011  ||  $array[0]>2012  ||  $array[1] > 12  ||  $array[2] >31)
{  error  }

You can also do whatever other checking you want regarding numeric, or year range.

0
Cornelia YoderArtistCommented:
http://us3.php.net/manual/en/function.explode.php

Note that if $date doesn't contain the hyphens properly, the explode() will not produce a 3 element array either, so even that case is covered by checking $array[2].
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wantabe2Author Commented:
Sorry about that.... here's a better formatting of the code I'm trying to use...thanks
<b> <p> <i> To go to the main page <a href="http://tnep-g-psrflow/flow/index.html">click here</a>.</p> </i>


<?php # edit_dqa.php

$page_title = 'Edit a Record';

$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("psrflow", $con);

ini_set('display_errors',1); 
error_reporting(E_ALL);


if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { 
	$id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { 
	$id = $_POST['id'];
} else { // No valid ID, kill the script.
	echo '<h1 id="mainhead">Page Error</h1>
	<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
	include ('./includes/footer.html'); 
	exit();
}

if (isset($_POST['submitted'])) {

	$errors = array(); 

	if (empty($_POST['pacts'])) {
		$errors[] = 'You forgot to enter your PACTS number. If there is no PACTS # you can enter 0000';
	} else {
		$pt = $_POST['pacts'];
	}
	
	if (empty($_POST['fname'])) {
		$errors[] = 'You forgot to enter the first name.';
	} else {
		$fn = $_POST['fname'];
	}
	
	if (empty($_POST['lname'])) {
		$errors[] = 'You forgot to enter the last name.';
	} else {
		$ln = $_POST['lname'];
	}

		if (empty($_POST['date_sub_crt'])) {
		$errors[] = 'You forgot to enter a date submitted to office!.';
	} else {
		$crt = $_POST['date_sub_crt'];
	}
	
		if (empty($_POST['date_disclo_att'])) {
		$errors[] = 'You forgot to enter the supervisor disclosure date!';
	} else {
		$disc = $_POST['date_disclo_att'];
	}
		
	if (empty($errors)) { 
		
	$query = "UPDATE psrinfo SET pacts='$pt', fname='$fn', lname='$ln', date_sub_crt='$crt', date_disclo_att='$disc' WHERE fid=$id";
	$result = @mysql_query ($query); // Run the query.
		
$EmailAddress = 'email@yahoo.com';	
$Message = "A clerk has entered a 'Date Submitted to supervisor or office'. Please enter/edit this information in PACTS.\n \n PACTS No : ${_REQUEST['pacts']}\n First Name : ${_REQUEST['fname']} \n Last Name : ${_REQUEST['lname']} \n Date Submitted to office : ${_REQUEST['date_sub_crt']} \n Date Dislosed to supervisor : ${_REQUEST['date_disclo_att']} ";	
$Subject  = "A Clerk has Edited a Record.";
$Headers = "From: PODDS@yahoo.com \n"; 

if (mail($EmailAddress, $Subject, $Message, $Headers)) {
				
echo "<p><center><h2>My Office</h2></center></p>";
echo "<p><h3><center>Eastern District</center></h3></p>";
echo "<center>An email has been sent to your supervisor for approval and "; 
 
} else { 
 
echo "This system is not working properly. Please contact IT so they can fix it."; 
 
} 		
		
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') OR (substr($url, -1) == '//')) {
$url = substr ($url, 0, -1);
}
$url ='/flow/index.html'; 

header("Location: $url");

exit();

		
	} else { // Report the errors.
	
		echo '<h1 id="mainhead">Error!</h1>
		<p class="error">The following error(s) occurred:<br />';
		foreach ($errors as $msg) { // Print each error.
			echo " - $msg<br />\n";
		}
		echo '</p><p>Please try again.</p><p><br /></p>';
		
	} 

} 

$query = "SELECT pacts, fname, lname, date_sub_crt, date_disclo_att, date_disclo_att FROM psrinfo WHERE fid = " . $_REQUEST['id'];
$result = @mysql_query ($query); // Run the query.

if (mysql_num_rows($result) == 1) { 

	$row = mysql_fetch_array ($result, MYSQL_NUM);
		
?>	

<?php

echo '<form action="clerk_edit.php" method="post">
<fieldset><legend><h2> You are editing a record! After submitting this form, your DQA will be notified of the change via email.</h2></legend>

<b>PACTS No:</b> <br><input type="text" name="pacts" size="15" maxlength="30" value="'.$row[0].'" /><br>
<b>First Name:</b> <br><input type="text" name="fname" size="15" maxlength="30" value="'.$row[1].'" /><br />
<b>Last Name:</b> <br><input type="text" name="lname" size="15" maxlength="30" value="'.$row[2].'" /><br />
<b>Submitted to office: </b><br><input type="text" name="date_sub_crt" size="15" maxlength="30" value="'.$row[3].'" /> <br>
<b>Submitted to supervisor: </b><br><input type="text" name="date_disclo_att" size="15" maxlength="30" value="'.$row[4].'" /> <br>
</fieldset>

<div align="left"><input type="submit" name="submit" value="Submit" /></div> 

<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="id" value="' . $id . '" />
</form>';

} else { // Not a valid user ID.
	echo '<h1 id="mainhead">Page Error</h1>
	<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
}

mysql_close(); // Close the database connection.

?>

Open in new window

0
wantabe2Author Commented:
Here's the code that checks to make sure something is entered & not left blank.....can I do something similair to this code to make sure the date is enetered in the YYYY-MM-DD format?

if (empty($_POST['date_disclo_att'])) {
		$errors[] = 'You forgot to enter the date!';
	} else {
		$disc = $_POST['date_disclo_att'];
	}

Open in new window

0
Cornelia YoderArtistCommented:
Sorry, was off having lunch, but I guess you figured it out.   You can just put the explode and checks following the $disc = $_POST line.

If you have any more questions with using it, I'll be happy to continue helping.

Cheers!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.