PHP To Not Display Grid if $Error Is Hit


I have the below syntax which works as it should and on the button press event will 1st verify that a begin date and end date has been input into the text boxes if one of them is blank a message writes on screen notifying the user that it is empty.

The issue with my syntax is that even though the error message displays on screen the syntax continues to iterate and the header row for the table is still displayed (and I assume if the query would return data the table would be populated with data as well). How should this syntax be altered so that if an error is displayed on screen the code stops once the error is displayed on screen?

$begindateerror = false;  $enddateerror = false;  if (empty($_POST['begindate'])) {  $begindateerror = true;  }  if (empty($_POST['enddate'])) {  $enddateerror = true;  }
if ($begindateerror) {  echo "Please select a begin date.
";  } else { }  if ($enddateerror) {  echo "Please select a end date.
";  } else { }
$option = array();
//Build option array for connection string
$db = JDatabase::getInstance( $option );  $tsql = $db->gettsql(true);  $tsql = "SQL tsql";
$db->settsql($tsql);   $tsql = $db->loadObjectList();
$numofrows = mssql_num_rows($tsql);
?>        //Create Header Row for the table        ";  //write the query results to the table  print "";  }}?>