Error Handling If A Record Is Not Found

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Antonio Bacasno
    New Member
    • Sep 2011
    • 22

    Error Handling If A Record Is Not Found

    I would like to kindly seek some coding assistance. I would like to put some kind of error handling in my code if, say, an ID number was entered, but that ID number has no existing record in the database. I have entered some kind of error-checking in my code that will redirect if an ID with no record was entered, but it just doesn't work. The code doesn't affect the page functionality if I entered an ID number with existing records, it only affects it if I entered an ID with no record. Please help. Thanks.


    Code:
    <?php require_once("includes/db_connect.php"); ?>
    <?php require_once("includes/functions.php"); ?>
    <?php
    	
    	$equipid = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['equipid']))); //equipid number coming from the updatedata.php
    	
    	$query = "SELECT * FROM tblequipmentmasterlist ";
    	$query .= "WHERE equipid =" . $equipid;
    	$query .= " LIMIT 1";
    	
    	$query_result = mysqli_query($db_cxn, $query);
    	
    	
    	if (mysqli_num_rows($query_result) == 0) {
    		header ("LOCATION : updatedata.php"); //If record doesn't exist
    	} else { //If a record do exist
    			while ($row = mysqli_fetch_array ($query_result)) {
    					$equipname = $row['equipname'];
    					$manufacturer = $row['manufacturer'];
    					$serno = $row['serno'];
    					$modelno = $row['modelno'];
    					$capacity = $row['capacity'];
    					$curloc = $row['curlocation'];
    					$equipcode = $row['equipcode'];
    					$seqno = $row['seqno'];
    					$adddate = $row['adddate'];
    				 }
    			}
    		
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>EST-Equipment Intranet-Calibration-Update Equipment Record</title>
    <meta http-equiv="Content-Language" content="English" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />
    <style type="text/css">
    <!--
    .style1 {
    	color: #000000;
    	font-size: medium;
    }
    
    #wrap #content .right .newequipment {
    	font-family: Verdana, Arial, Helvetica, sans-serif;
    	font-size: 14px;
    	font-style: normal;
    	font-weight: lighter;
    	text-transform: capitalize;
    	text-align: left;
    	text-indent: 5px;
    	margin: 10px;
    	padding: 10px;
    	width: 350px;
    }
    -->
    </style>
    
    <?php include("includes/calheader.php"); ?>
    
    <h2>Update Equipment Information Form</h2>
    <form action = "processform_update.php" method = "POST">
    <div class="DataInput">
      <table width="707" border="0" cellpadding="0" cellspacing="0" id="Equipname6">
        <tr>
          <td width="200" class="style1">Equipment ID No.</td>
          <td width="507"><input name="equipid" type="text" id="equipid" tabindex="" value="<?php echo $equipid; ?>" size="7" />
         </tr>
      </table>
    </div>
    <div class="DataInput">
     <table width="707" border="0" cellpadding="0" cellspacing="0" id="Equipname">
        <tr>
          <td width="200" class="style1">Equipment Name</td>
          <td width="507">
            <input name="equipname" type="text" id="equipname" tabindex="1" value="<?php echo $equipname; ?>" size="40" />
          </td>
        </tr>
      </table>
    </div>
    <div class="DataInput">
     <table width="708" border="0" cellpadding="0" cellspacing="0" id="Equipname2">
        <tr>
          <td width="200" class="style1">Manufacturer</td>
          <td width="508">
            <input name="manufacturer" type="text" id="manufacturer" tabindex="2" value="<?php echo $manufacturer; ?>" size="40" />
          </td>
        </tr>
      </table>
    </div>
    <div class="DataInput">
     <table width="561" border="0" cellpadding="0" cellspacing="0" id="Equipname3">
        <tr>
          <td width="200" class="style1">Serial No. (if any)</td>
          <td width="361">
          <input name="serno" type="text" id="serno" tabindex="3" value="<?php echo $serno; ?>" size="30" />
          </td>
        </tr>
      </table>
    </div>
    <div class="DataInput">
     <table width="561" border="0" cellpadding="0" cellspacing="0" id="Equipname4">
        <tr>
          <td width="200" class="style1">Model No. (if any)</td>
          <td width="361">
          <input name="modelno" type="text" id="modelno" tabindex="4" value="<?php echo $modelno; ?>" size="30" />
          </td>
        </tr>
      </table>
    </div>
    <div class="DataInput">
      <label class="style6"></label>
      <table width="707" border="0" cellpadding="0" cellspacing="0" id="Equipname5">
        <tr>
          <td width="200" class="style1">Capacity / Range</td>
          <td width="507">
          <input name="capacity" type="text" id="capacity" tabindex="5" value="<?php echo $capacity; ?>" size="40" />
          </td>
        </tr>
      </table>
    </div>
    <div class="DataInput">
    	<label class="style6"></label>
      <table width="707" border="0" cellpadding="0" cellspacing="0" id="Equipname6">
        <tr>
          <td width="200" class="style1">Current Location</td>
          <td width="507">
          <input name="curlocation" type="text" id="curlocation" value="<?php echo $curloc; ?>" tabindex="6" size="20" />
          <a href='locationcode.php' target='_blank' class='style7'>...Click here for definition...</a> 
          </td>
        </tr>
      </table>
    </div>
    <div class="DataInput">
    	<label class="style6"></label>
      <table width="707" border="0" cellpadding="0" cellspacing="0" id="Equipname5">
        <tr>
          <td width="200" class="style1">Equipment Code</td>
          <td width="507">
          <input name="equipcode" type="text" id="equipcode" value="<?php echo $equipcode; ?>" tabindex="7" size="20" />
          <a href='equipmentcode.php' target='_blank' class='style7'>...Click here for definition...</a> 
          </td>
        </tr>
      </table>
    </div>
    <div class="DataInput">
     <table width="561" border="0" cellpadding="0" cellspacing="0" id="Equipname8">
        <tr>
          <td width="200" class="style1">Sequence Number</td>
          <td width="361">
          <input name="seqno" type="text" id="seqno" tabindex="8" size="7" maxlength="7" value="<?php echo $seqno; ?>" />
          </td>
        </tr>
      </table>
    </div>
    <div class="DataInput">
     <label class="style6"></label>
      <table width="707" border="0" cellpadding="0" cellspacing="0" id="Equipname5">
        <tr>
          <td width="200" class="style1">Date Added</td>
          <td width="507">
          <input name="adddate" type="text" id="adddate" tabindex="9" value="<?php echo $adddate; ?>" size="20" /> 
          <strong>YYYY-MM-DD</strong> </td>
        </tr>
      </table>
    </div>
    <p>&nbsp;</p>
    <div class="DataInput">
    <input type="submit" name="submit" id="submit" value="Submit" />
    </div>
    </form>
    <p><br />
    <br />
      <br />
      <br />
      <br />
      </div>
      
    <?php require("includes/footer.php"); ?>
    Also, I would like the EQUIPID text field on this page to be uneditable. I tried DISABLING it, but when I submit the data of this page to be processed, it shows an UNDEFINED INDEX error of my EQUIPID, seems like the EQUIPID variable is not being passed on to the PROCESSING page. I would like the EQUIPID text field to be uneditable so no one can mistakenly edit/update it. Thanks in advance.
  • Antonio Bacasno
    New Member
    • Sep 2011
    • 22

    #2
    I have finally figured out the problem. It was all syntax. I've put an extra space after LOCATION (header), which PHP doesn't accept. It is now working.

    Comment

    Working...