How much elseif's are enough!

Hello Forum members!

I have this php code how can I modify it to display the result which I am expecting?

<?php include 'includes/dbConnect.inc.php';

	$conn = dbConnect('query');
	
	if(array_key_exists('Submit', $_POST)){
		$surname = htmlspecialchars($_POST['surname']);
		$driverRegNo = htmlspecialchars($_POST['driverRegNo']);
		
		if(isset($surname)){
			$searchitem = htmlspecialchars($_POST['searchitem']);
			$query = "SELECT *
  					FROM drivers
 					WHERE surname LIKE '%$searchitem%' 
   				 ORDER BY surname";
				 $numresults=mysql_query($query) or die(mysql_error());
 				$numrows=mysql_num_rows($numresults);
				
				
		}
	
		elseif(isset($driverRegNo)){
			$searchitem = htmlspecialchars($_POST['searchitem']);
			$query = "SELECT *
  					FROM drivers
 					WHERE driverRegNo LIKE '%$searchitem%' 
   				 ORDER BY surname";
				 $numresults=mysql_query($query) or die(mysql_error());
 				$numrows=mysql_num_rows($numresults);
		}//ongezwad*/
		
		elseif($numrows == 0){
		
				echo 'Your search for      <b>' .$searchitem. '</b>        could not be found';
				}else{?>
					<!--/*while($row = mysql_fetch_assoc($numresults)){
					 echo 'You searched for '. $row['surname'];
					}*/-->
					<table id="main">
    <tr>
    
    <th>DRIVER LICENCE No.</th>
    <th>SURNAME</th>
    <!--<th>OTHERNAMES</th>-->
    <th>GENDER</th>
    <!--<th>ADDRESS</th>-->
    <th> TELEPHONE No.</th>
    <!--<th>PLACE OF BIRTH</th>
    <th>NATIONALITY</th>-->
    <!-- <th>SCHOOL ATTENDED</th>
      <th>LICENCE ISSUE DATE</th>
       <th>LICENCE CLASS</th>
        <th>ISSUING AUTHORITY</th>
         <th>LICENCE EXPIRY DATE</th>-->
    
        <!--<th scope="col">driver Reg NO</th>
        <th scope="col">surname</th>
        <th scope="col">othernames</th>
        <th scope="col">gender</th>
		<th>&nbsp;</th>
		<th>&nbsp;</th>-->
    </tr>
	<?php
	while($row = mysql_fetch_assoc($numresults)) {
	?>
    <tr>
         <td id="midcontent"><?php echo $row['driverRegNo']; ?></td>
        <td id="midcontent"><?php echo $row['surname']; ?></td>
      <!--  <td id="midcontent"><php echo $row['othernames']; ?></td>-->
        <td id="midcontent"><?php echo $row['gender']; ?></td>
        <!--<td id="midcontent"><php echo $row['address']; ?></td>-->
        <td id="midcontent"><?php echo $row['telephoneNo']; ?></td>
        <!--<td id="midcontent"><php echo $row['placeOfBirth']; ?></td>
        <td id="midcontent"><php echo $row['nationality']; ?></td>-->
       <!-- <td id="midcontent"><php echo $row['schoolName']; ?></td>
        <td id="midcontent"><php echo $row['issueDate']; ?></td>
        <td id="midcontent"><php echo $row['licenceClass']; ?></td>
        <td id="midcontent"><php echo $row['issuingAuthority']; ?></td>
        <td id="midcontent"><php echo $row['expiryDate']; ?></td>-->
		<td id="midcontent"><a href="Update_Drivers.php?driverID=<?php echo $row['driverID']; ?>">Edit</a></td>
		<td><a href="delete_driver.php?driverID=<?php echo $row['driverID']; ?>">Remove</a></td>
    </tr>
	<?php } ?>
</table>
			<?php	}
	}
	

?>

and here is its html form


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form action="" method="post" name="searchform" id="">
 <div> <label for="searchdata">Select Criteria for Search</label>
  <select name="searchdata" id="searchdata">
    <option value="surname">Driver's Surname</option>
    <option value="driverRegNo">Driver's Licence No.</option>
      </select>
  </div>
  <div>
      <label for="searchdata">Type your search criteria</label><input type="text" name="searchitem" id="searchitem" />
  </div>
  <div>
  	<input name="Submit" type="submit" value="Search" />
  </div>
</form>
</body>
</html>


thank you, inadvance

Couple of things.

In your form, you try to set a label searchdata twice.

In your postback form handler the first thing you do is check for the existence of the submit button being pressed - I never bother with that, sometimes users press Enter - check for the existence of the key element of your form being filled in ie “searchitem” (otherwise put a hidden field in there).

I mean in your case, if they don’t fill in a ‘searchitem’ your whole script is just wasting time and energy, isnt it?

Consider doing this at the top:


<?php
## lets fail early ##

if( ! isset($_POST['searchitem'] ) || strlen( trim($_POST['searchitem'])) < 3 ){
// send them back to the form. You can get jiggy with
// this by sending error details if you want to - later
header( 'Location: /form.php')
exit() 
}

// so at least we know they sent a string > 3 chars and submitted the form
// or later on, you decided to use some JS to submit the form AFTER checking
// some min requirements on the client, say.

// add trim() to get rid of trailing/leading white space
$searchitem = htmlspecialchars(trim($_POST['searchitem']));

Then boil down those 2 sql statements and associated code to one

DRY - do not repeat yourself



// set the default search type here
 $where = "surname";

// ONLY change it if they chose the second one
// (I think this is right....)
if( isset($_POST['searchdata']) && $_POST['searchdata'] == "driverRegNo" ) {
 $where = "driverRegNo";
}

// hence only do this once

          $query = "SELECT * FROM drivers
          WHERE $where LIKE '%$searchitem%' 
          ORDER BY surname";

             $numresults=mysql_query($query) or die(mysql_error());
             $numrows=mysql_num_rows($numresults);

// and so on ...

HTH

Thank you!