Location: PHPKode > projects > PhpMyInventory > pmi281/admin_peripherals.php
<?
  Include("Includes/global.inc.php");
  checkPermissions(1, 1800);

// Has the form been submitted
if ($btnSubmit) {
  // are required fields were filled out
  if ($peripheral_fk != "" AND $fk_asset != "") {

    $serial_num      =  validateText("Serial Number", $txtSerial, 2, 35, FALSE, FALSE);
    $external        =  validateText("External (Y/N)", $radExternal, 1, 1, TRUE, FALSE);

    if (!$strError AND !$pk_peripheral AND $serial_num) {
        $strSQLerr = "SELECT COUNT(*) FROM peripherals WHERE peripheral_fk=$peripheral_fk AND
          accountID=$accountID AND serial_num='$serial_num'";
    } elseif (!$strError AND $pk_peripheral AND $serial_num) {
        $strSQLerr = "SELECT COUNT(*) FROM peripherals WHERE peripheral_fk=$peripheral_fk AND
          accountID=$accountID AND serial_num='$serial_num' AND pk_peripheral!=$pk_peripheral";
    }
    If (!$strError AND $serial_num) {
        $resulterr = dbquery($strSQLerr);
        $rowerr = mysql_fetch_row($resulterr);
        If ($rowerr[0] > 0) {
            $strError = "That serial number already exists.";
        }
    }

    if (!$strError) {
        // Pick the proper sql statement to query the database
        if ($pk_peripheral) {
           $strSQL = "UPDATE peripherals SET serial_num='$serial_num',external='$external',fk_asset='$fk_asset',peripheral_fk='$peripheral_fk' WHERE accountID=$accountID AND pk_peripheral=$pk_peripheral";
           $strError = "Record updated successfully.";
        } else {
           $strSQL = "INSERT INTO peripherals (pk_peripheral,fk_asset,serial_num,peripheral_fk,external,accountID) VALUES ('0','$fk_asset','$serial_num','$peripheral_fk','$external', $accountID)";
           $strError = "Record created successfully.";
        }
        $result = dbquery($strSQL);
        $noShow = 1;

        // get the new primary key for peripheral table
        if (!$pk_periferal) { $pk_periferal = mysql_insert_id(); }
    } else {
        $keepUserText = TRUE;
    }

  } else {
    // set not all there flag
    $strError = "Error: Be sure you haven't altered the querystring.";
  }
}

// If you're editing preload the vars
if ($pk_peripheral != "" AND !$delete) {
  // find the peripheral we're looking for
  $strSQL = "SELECT * FROM peripherals as p, peripheral_types as pt WHERE
    p.peripheral_fk=pt.peripheral_pk AND p.pk_peripheral=$pk_peripheral AND
    pt.accountID=$accountID";
  $result = dbquery($strSQL);

  $row = mysql_fetch_array($result);
  $pk_peripheral = $row["pk_peripheral"];
  $fk_asset      = $row["fk_asset"];
  $peripheral_fk = $row["peripheral_fk"];
  If (!$keepUserText) {
      $serial_num    = $row["serial_num"];
      $external      = $row["external"];
  }
  $manufacturer  = $row['manufacturer'];
  $model         = $row['model'];
  $description   = $row['description'];

  $actionWord = "Edit";
} Else {
  $actionWord = "Add";
}

if ($fk_asset == "" AND !$delete){
  $strError = "Error: Be sure you haven't altered the querystring.";
}

writeHeader($actionWord." a Peripheral");
declareError(TRUE);

if ($fk_asset != "" AND $peripheral_fk != "") {
      $strSQL = "SELECT * FROM peripheral_types WHERE peripheral_pk=$peripheral_fk
        AND accountID=$accountID";
      $result = dbquery($strSQL);

      $row = mysql_fetch_array($result);
      $manufacturer = $row['manufacturer'];
      $model        = $row['model'];
      $description  = $row['description'];
?>
  <font color='ff0000'>*</font> Indicates a required field.<p>

  <FORM METHOD="post" ACTION="<? echo $PHP_SELF ?>">
  <INPUT TYPE="hidden" NAME="pk_peripheral" VALUE="<? echo $pk_peripheral; ?>">
  <INPUT TYPE="hidden" NAME="fk_asset" VALUE="<? echo $fk_asset; ?>">
  <INPUT TYPE="hidden" NAME="peripheral_fk" VALUE="<? echo $peripheral_fk; ?>">
  <INPUT TYPE="hidden" NAME="uid" VALUE="<? echo $uid; ?>">
  <table border='0' cellpadding='3' width='100%'>
    <tr>
      <td width='106'><u>Product Name</u>:
      </td>
      <td width='450'><? echo $description; ?>
      </td>
    </tr>
    <tr>
      <td width='106'><u>Manufacturer</u>:
      </td>
      <td width='450'><? echo $manufacturer; ?>
      </td>
    </tr>
    <tr>
      <td width='106'><u>Model</u>:
      </td>
      <td width='450'><? echo $model; ?>
      </td>
    </tr>
    <tr>
      <td width='106'><u>Serial Number</u>:
      </td>
      <td width='450'><INPUT SIZE="30" MAXSIZE="50" TYPE="Text" NAME="txtSerial" VALUE="<? echo antiSlash($serial_num); ?>">
      </td>
    </tr>
    <tr>
      <td width='106' valign='top'><font color='ff0000'>*</font> <u>External</u>?
      </td>
      <td width='450'>
        <INPUT TYPE="radio" NAME="radExternal" VALUE="Y" <?echo writeChecked("Y", $external)?>> Yes<br>
        <INPUT TYPE="radio" NAME="radExternal" VALUE="N" <?echo writeChecked("N", $external)?>> No
      </td>
    </tr>
    <tr>
      <td colspan='2'>&nbsp;
      </td>
    </tr>
     <tr>
      <td colspan='2'><? If (!$noShow) { ?><INPUT TYPE="submit" NAME="btnSubmit" VALUE="Enter Information"><? } ?>
      &nbsp;</td>
    </tr>
  </table>
  </FORM>
<?
  If ($uid) {
      buildlist($fk_asset, $uid, 1);
  } 
}

if ($peripheral_fk == "") {
  // if this is the first time we've entered this page then get the user
  // to select a peripheral type

?><P>
<A HREF="admin_peripheral_types.php">Add New Type</A>
</P><?

  $strSQL = "SELECT * FROM peripheral_types WHERE accountID=$accountID ORDER BY description ASC";
  $result = dbquery($strSQL);

?>
   <table border='0' cellpadding='4' cellspacing='0'>
     <TR class='title'>
       <TD><b>Description</b></TD>
       <TD><b>Model</b></TD>
       <TD><b>Manufacturer</b></TD>
       <TD><b>Action</b></TD>
     </TR>
<?
  while ($that = mysql_fetch_array($result)) {
    $peripheral_fk = $that['peripheral_pk'];
    $manufacturer  = $that['manufacturer'];
    $model         = $that['model'];
    $description   = $that['description'];

?>
  <TR class='<?=alternateRowColor()?>'>
     <TD><? echo $description ?> &nbsp;</TD>
     <TD><? echo $model ?> &nbsp;</TD>
     <TD><? echo $manufacturer ?> &nbsp;</TD>
     <TD>
       <A HREF="admin_peripherals.php?fk_asset=<? echo $fk_asset; ?>&peripheral_fk=<? echo $peripheral_fk; ?>&uid=<? echo $uid; ?>">Add</A>
     </TD>
  </TR>
<?
  }
?></table><?
}

writeFooter();
?>
Return current item: PhpMyInventory