Location: PHPKode > projects > PAT and Inventory Management System > m/a/add_pat.php
<!--
	Pat and Inventory Management System
	Version 0.1
	Created by Tom Dyer
	Last edited by Tom Dyer 12/9/09
	Inventory - Add an Item
-->
<?php
session_start();	// This connects to the existing session. Allows sharing of variables between pages for a smoother user experience
$directory = $_SESSION['docroot'];
include_once '../../config.php';
$barcode = $_SESSION['barcode'];
$seluser = $_SESSION['returnuser'];	//if it has not, use the session variable for pre-selected user

mysql_connect($sqlhost,$username,$password);						//connect to the database
	@mysql_select_db($database) or die( "Unable to select database");
	$query="SELECT * FROM users ORDER BY name ASC";						//get a list of users
	$result=mysql_query($query);
	
	$num=mysql_numrows($result);
	
?>
<html>
	<title>PIMS | Add an Item & Log Test</title>
	<head><link rel="shortcut icon" href="<?php echo "$servroot"; ?>/favicon.ico" type="image/x-icon"><h4>Add an Item and Test to the Database</h4></head>
	<body onLoad="document.input.name.focus()" LINK="0033CC" VLINK="0033CC"> <!-- Keep links blue after visit -->
	
		<table border="0"> <!-- Table to make the form look nice -->
		<form action="" method="post" name="input"> <!-- Form that will submit data back to this file -->
		<input type="hidden" name="cmd" value="done"</input>
		<tr>
		<td>Barcode: </td><td><?php echo "$barcode"; ?></td>
		</tr><tr>
		<td>Item Description: </td><td><input id="name" name="name" value = "<?php echo "$name"; ?>"/></td>
		</tr><tr>
		<td>Retest Period: </td><td><input id="retest" name="retest" size="2" value="1"> Year(s)</td>
        </tr><tr>
		<td>Tested By: </td><td><SELECT id="user" name="user"><option value=pick>Choose Name</option><?php
														$i=0;
														while ($i < $num) {
														$names=mysql_result($result,$i,"name");
														echo '<option value="'.$names.'" ';
															if ($names == $seluser){
															echo ' selected="selected"';
															} else {}
														echo '>'.$names.'</option>';
														$i++;
														}
														?></select></td><!-- Name feild in the form of a drop-down-box. Names found via SQL, and the last entered name is remembered so remains selected. -->
		</tr><tr>
		<td>Test Date<small>(Y-M-D)</small>: </td><td><input id="date" name="date" value="<?php echo date("Y-m-j"); ?>"/></td><!-- Date Feild. Date auto-fills with the current. -->
		</tr><tr>
		
		<td><input type="submit" value="Submit"></td>
		</tr>
		</form> <!-- Close of Input Form -->
		</table> <!-- Close of Form Table -->
		
		<?php

	if ($_POST['cmd'] == 'done') { //check if the form has been submitted
		
		$name = $_POST['name']; //set variables
		$retest = $_POST['retest'];
		$user = $_POST['user'];
		$date = $_POST['date'];
		$barcode = (int)$barcode; // Format out any leading zeros and/or text
		if(empty($name)) // Check for a Description
			{
				echo "Please enter a Description";
				echo '<br><hr width="100%" /><a href="index.php">Back</a><br>';
				die;
			}
		if($user == 'pick') // Check if user has entered a name
			{
				echo "Please select your Name"; // Tell them what they did wrong
				echo '<br><hr width="100%" /><a href="index.php">Back</a><br>'; //Navigation Options
				die; //Stop
			}
		mysql_connect($sqlhost,$username,$password);		
		@mysql_select_db($database) or die( "Unable to select database"); // Connect to the database to check for a previous record
		$data = mysql_query("SELECT * FROM equipment WHERE barcode='$barcode'");
		while($row = mysql_fetch_array($data))
			{
				$gotbarcode=$row['barcode'];
				$gotname=$row['name'];
			  	$gotretest=$row['retest'];
			}
		if(empty($gotname)) // check for an existing record
			{
				$newdate=$date;	// Data adjust (if required)
				mysql_connect($sqlhost,$username,$password);		
				@mysql_select_db($database) or die( "Unable to select database");
				mysql_query("INSERT INTO `equipment` VALUES ('$barcode', '$name', '$retest', '$newdate', '$user')"); // Enter new information into the Database
					
				echo "$name has been successfully added to the database."; // Tell the user it worked
				echo '<br><hr width="100%" /><a href="..">Back</a><br><a href="../../../">Home</a><br>'; // Give them navigation
				echo '<script language="javascript">window.location="index.php";</script>';
				die; // Stop
			}
		}
?> 

		
		<br><hr width="100%" /><a href="index.php">Back</a><br>
	</body>
<html>
Return current item: PAT and Inventory Management System