PHP error handling with exit

I believe I’ve tried this, as in the code below and although the footer is visible, the conditional checks do not work as expected. Again, the check for blank space dumps a large amount of db data (and displayed incorrectly) into the page and the alphanumeric check does not honour all symbols.

You’ll have to bear with me here, I’m a little out of my depth

The code:

<?php $searchtext = mysqli_real_escape_string($connection, $_POST['search']); $errors = array(); if (!preg_match('/^[a-zA-Z0-9 ]*$/', $_POST['search'])){ //echo $error = 'Only alphanumerics allowed.'; //exit(); $errors['value1'] = "alpha"; } if (empty($_POST['search'])){ //echo $error = 'Please enter a search term.'; //exit(); $errors['value2'] = "blank"; } $sql = "SELECT text.* FROM text WHERE output LIKE '%".$searchtext."%' OR altText LIKE '%".$searchtext."%'"; $query=mysqli_query($connection, $sql); $result=mysqli_fetch_assoc($query); if(mysqli_num_rows($query)>0) { do { ?>
							<ul>
							<li>
								<?php 
									
									echo $result['outputURL'];
									echo preg_replace("/(".$searchtext.")/i", "<span class='highlight'>$1</span>", $result['output']);	
								?>
							</li>
							</ul>
								<?php 
								
									} while ($result=mysqli_fetch_assoc($query));
							
										} else {
						
											echo $error = 'Please refine your search term.';
										}
							?>
							
							
							
																
									</div>	
										
										
										
										</section>
										
									</article>
							
								
							
							</div>

					</div>
				</div>
			</div>
		</div>
		
	<?php
		
		
		function form_errors($errors=array()) {
			$output = "";
			if (!empty($errors)) {
			  $output .= "<div class=\"error\">";
			  $output .= "Please fix the following errors:";
			  $output .= "<ul>";
			  foreach ($errors as $key => $error) {
			    $output .= "<li>{$error}</li>";
			  }
			  $output .= "</ul>";
			  $output .= "</div>";
			}
			return $output;
		}
	?>
	<?php echo form_errors($errors); ?>
	
	
		<?php mysqli_close($connection); ?>

	<?php include ($_SERVER['DOCUMENT_ROOT'].'/include/footer.php'); ?>
	
	<?php exit(); ?>