Location: PHPKode > projects > Helpdesk Over Web > how/users/newuser.php
<?php

// Purpose : Allow creation of new user database record.

// Include Configuration File
require_once ('../includes/config.inc'); 
require_once ('../includes/mysql_connect.php'); // Connect to the database.

// Set the page title and include the HTML header.
$page_title = 'Helpdesk Over Web - Add User Record';
include ('../includes/header.html');

// Check if form is submitted or blank.
if (isset($_POST['submit'])) // Check if the form has been submitted.
	{
	require_once ('../includes/mysql_connect.php'); // Connect to the database.

	// Retrieve variable values from form.
	
	$userid = $_POST['userid'];
	$firstname = $_POST['firstname'];
	$surname = $_POST['surname'];
	$dept = $_POST['dept'];
	$site = $_POST['site'];
	$tel = $_POST['tel'];
	$email = $_POST['email'];

	// Check compulsory fields are completed.
	if($userid == NULL)
		echo "<H4><FONT COLOR='red'>Error - No User ID specified!</FONT></H4>";
	if($firstname == NULL)
		echo "<H4><FONT COLOR='red'>Error - No First Name specified!</FONT></H4>";
	if($surname == NULL)
		echo "<H4><FONT COLOR='red'>Error - No Surname specified!</FONT></H4>";
	if($dept == NULL)
		echo "<H4><FONT COLOR='red'>Error - No Department specified!</FONT></H4>";
	if($site == NULL)
		echo "<H4><FONT COLOR='red'>Error - No Site specified!</FONT></H4>";
	
	// If all OK, insert new user into database.
	if($userid && $firstname && $surname && $dept && $site)
	{
		$query = "INSERT INTO users (UserID, UserFirstName, UserSurname, Dept, Site, Tel, Email) VALUES ('$userid', '$firstname', '$surname', '$dept', '$site', '$tel', '$email');";
		$result = mysql_query($query)
		   or die("Invalid query: " . mysql_error());
		echo ("<H2>Operation Complete.</H2>");  
		echo ("<H4>User " . $firstname . " " . $surname . " (" . $userid . ") created.</H4>");
		echo ("<H4><A HREF='useradmin.php?sortorder=UserSurname'>[ Main User Administration ]</A></H4>");

		// Include standard HTML footer
		include ('../includes/footer.html');
		exit();
	}
}
	// Output text/instructions for user.
	echo "<H1>Users Database</H1>";
	echo "<H3>Create New User</H3>";
	echo "<P>Enter user details in the fields below.</P>";
	echo "<TABLE WIDTH='500' PADDING='2' SPACING='2'>";
	echo "<form action='newuser.php' method='post'>";
	echo "<TR><TD ALIGN='RIGHT'><B>User ID:<FONT COLOR='red' SIZE='-2'><B>*</B></FONT></B></TD><TD>&nbsp;&nbsp;<input type='text' name='userid' size='16' maxlength='14' class='textbox'></TD></TR>";
	echo "<TR><TD ALIGN='RIGHT'><B>First Name:<FONT COLOR='red' SIZE='-2'><B>*</B></FONT></B></TD><TD>&nbsp;&nbsp;<input type='text' name='firstname' size='60' maxlength='50' class='textbox'></TD></TR>";
	echo "<TR><TD ALIGN='RIGHT'><B>Surname:<FONT COLOR='red' SIZE='-2'><B>*</B></FONT></B></TD><TD>&nbsp;&nbsp;<input type='text' name='surname' size='60' maxlength='50' class='textbox'></TD></TR>";
	echo "<TR><TD ALIGN='RIGHT'><B>Dept:<FONT COLOR='red' SIZE='-2'><B>*</B></FONT></B></TD><TD>&nbsp;&nbsp;<SELECT NAME='dept'>";
	$deptsql = "SELECT DeptName FROM depts ORDER BY DeptName;";
	$deptlist = mysql_query($deptsql)
	   or die("Invalid query: " . mysql_error());
	while($i = mysql_fetch_row($deptlist)) 
		{ 
		echo "<OPTION >$i[0]</OPTION>";
		}
	echo "</SELECT></TD></TR>";
	echo "<TR><TD ALIGN='RIGHT'><B>Site:<FONT COLOR='red' SIZE='-2'><B>*</B></FONT></B></TD><TD>&nbsp;&nbsp;<SELECT NAME='site'>";
	$sitesql = "SELECT SiteName FROM sites ORDER BY SiteName;";
	$sitelist = mysql_query($sitesql)
	   or die("Invalid query: " . mysql_error());
	while($i = mysql_fetch_row($sitelist)) 
		{ 
		echo "<OPTION >$i[0]</OPTION>";
		}
	echo "</SELECT></TD></TR>";
	echo "<TR><TD ALIGN='RIGHT'><B>Telephone:</B></TD><TD>&nbsp;&nbsp;<input type='text' name='tel' size='18' class='textbox'></TD></TR>";
	echo "<TR><TD ALIGN='RIGHT'><B>Email:</B></TD><TD>&nbsp;&nbsp;<input type='text' name='email' size='40' class='textbox'></TD></TR>";
	echo "<TR><TD ALIGN='CENTER' COLSPAN='2'><input type='submit' value=' Create User Account ' name='submit'>&nbsp;&nbsp;<input type='reset' value='Clear'></TD></TR>";
	echo "</TABLE></FORM>";
	echo "<FONT COLOR='red' SIZE='-2'><B>* INDICATES REQUIRED FIELD</B></FONT>";
	echo "<H4>[ <A HREF='useradmin.php?sortorder=UserSurname''>User Administration Main Page</A> ]</H4>";

// Include standard HTML footer
include ('../includes/footer.html');
?>
Return current item: Helpdesk Over Web