<?php
ini_set('display_errors' , 0);
require_once('config.php');
//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","500");
//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
//This variable is used as a flag. The value is initialized with 0 (meaning no error found)
//and it will be changed to 1 if an errro occures.
//If the error occures the file will not be uploaded.
$errors=0;
//checks if the form has been submitted
if(isset($_POST['Submit']))
{
//reads the name of the file the user submitted for uploading
$image=$_FILES['image']['name'];
//if it is not empty
if ($image)
{
//get the original name of the file from the clients machine
$filename = stripslashes($_FILES['image']['name']);
//get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
//if it is not a known extension, we will suppose it is an error and will not upload the file,
//otherwise we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
//print error message
echo '<h1>Unknown extension!</h1>';
$errors=1;
}
else
{
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file
//in which the uploaded file was stored on the server
$size=filesize($_FILES['image']['tmp_name']);
//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}
//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname="images/".$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
{
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}}}}
//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors)
{
//echo "<h3>File Uploaded Successfully!</h3>"."File name is ".$newname;
//$con1=mysql_connect("localhost","root",""); mysql_select_db("shoes");
mysql_query("INSERT INTO image(imageId, newname)VALUES('$imageId', '$image_source')");
?>
<?php
$query="SELECT * FROM employees";
$result=mysql_query($query)or die("Could Not Execute Query");
$sql=mysql_query("SELECT * FROM employees WHERE employeeId='$employeeId' ");
$result=mysql_fetch_assoc($sql);
$rows = mysql_num_rows($sql);
if(isset($_POST['submit'])){
$image_source=$_POST['newname'];
$employeeId=$_POST['employeeId'];
$full_name=$_POST['full_name'];
$full_name=mysql_real_escape_string($full_name);
$full_name=ucfirst($full_name);
if($rows==1){ echo "<br><font color='red'>Employee Id Already Existed!</font>"; }
else
if($rows==0){
$designation=$_POST['designation'];
$gender=$_POST['gender'];
$dob=$_POST['dob'];
$address=$_POST['address'];
$address=mysql_real_escape_string($address);
$address=ucfirst($address);
$nok=$_POST['nok'];
$nok=mysql_real_escape_string($nok);
$nok=ucfirst($nok);
$nok_phone=$_POST['nok_phone'];
$referee=$_POST['referee'];
$referee=mysql_real_escape_string($referee);
$referee=ucfirst($referee);
$referee_phone=$_POST['referee_phone'];
$hire_date=$_POST['hire_date'];
$dt=date('Y-m-d');
mysql_query("INSERT INTO employees(newname, employeeId, full_name, designation, gender, dob, address, nok, nok_phone, referee, referee_phone, hire_date, dt)VALUES('$image_source', '$employeeId', '$full_name', '$designation', '$gender', '$dob', '$address', '$nok', '$nok_phone', '$referee', '$referee_phone', '$hire_date', '$dt')");
}
}
}
?>
<!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>
<p><form action="" method="post" enctype="multipart/form-data" name="newad" id="newad">
<p align="center">
<label></label>
<label></label></p>
<table width="290" border="0" align="center">
<tr>
<td width="10"> </td>
<p>
<td width="300"><img src='<?php echo $newname; ?>' alt="" width="164" height="116" /></td>
<p>
</tr>
<tr>
<td> </td><p>
<td><input type="file" name="image" /></td><p>
</tr>
<tr>
<td></td>
<td><input name="Submit" type="submit" value="Upload image" /></td>
</tr>
</table>
<p><td><input name="image_source" type="hidden" id="image_source" value="<?php echo $newname; ?>" readonly="readonly"/></span></label>
</span></td></p>
</form>
<form action="" name="" method="post">
<table border="0" align="center">
<tr>
<td>Full Name:</td>
<td><input name="full_name" type="text" id="full_name" /></td>
</tr>
<tr valign="baseline">
<td nowrap align="left">Designation:</td>
<td><select name="designation"id="designation">
<option selected="selected">Select</option>
<?php
$sql=mysql_query("SELECT * FROM designation");
$result=mysql_fetch_assoc($sql);
do{ ?>
<option value="<?php echo $result['designation'];?>"><?php echo $result['designation'];?></option>
<?php
} while ($result=mysql_fetch_assoc($sql));
?>
</select> </td>
</tr>
<tr valign="baseline">
<td nowrap align="right"><div align="left">Gender:</div></td>
<td><select name="gender"id="gender">
<option selected="selected">Select</option>
<?php
$sql=mysql_query("SELECT * FROM gender");
$result=mysql_fetch_assoc($sql);
do{ ?>
<option value="<?php echo $result['gender'];?>"><?php echo $result['gender'];?></option>
<?php
} while ($result=mysql_fetch_assoc($sql));
?>
</select> </td>
</tr>
<tr>
<td>Date of Birth:</td>
<td><input name="dob" type="text" id="dob" /></td>
</tr>
<tr>
<td>Address:</td>
<td><input name="address" type="text" id="address" /></td>
</tr>
<tr>
<td>Next of Kin:</td>
<td><input name="nok" type="text" id="nok" /></td>
</tr>
<tr>
<td>Next of Kin Phone:</td>
<td><input name="nok_phone" type="text" id="nok_phone" /></td>
</tr>
<tr>
<td>Referee:</td>
<td><input name="referee" type="text" id="referee" /></td>
</tr>
<tr>
<td>Referee Phone:</td>
<td><input name="referee_phone" type="text" id="referee_phone" /></td>
</tr>
<tr>
<td>Hire Date:</td>
<td><input name="hire_date" type="text" id="hire_date" /></td>
</tr>
<tr>
<td></td>
<td><input name="submit" type="submit" id="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
And here is the database:
-- Database: `shoes`
--
-- --------------------------------------------------------
--
-- Table structure for table `designation`
--
CREATE TABLE IF NOT EXISTS `designation` (
`designationId` int(11) NOT NULL,
`designation` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `designation`
--
INSERT INTO `designation` (`designationId`, `designation`) VALUES
(1, 'Managing Director'),
(2, 'Director'),
(3, 'Sales Manager'),
(4, 'Engineer'),
(5, 'Supervisor-I'),
(6, 'Supervisor-II'),
(7, 'Driver'),
(8, 'Painter');
-- --------------------------------------------------------
--
-- Table structure for table `employees`
--
CREATE TABLE IF NOT EXISTS `employees` (
`newname` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`employeeId` int(11) NOT NULL AUTO_INCREMENT,
`full_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`designation` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
`gender` varchar(6) COLLATE utf8_unicode_ci NOT NULL,
`dob` date NOT NULL,
`address` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
`nok` varchar(35) COLLATE utf8_unicode_ci NOT NULL,
`nok_phone` varchar(16) COLLATE utf8_unicode_ci NOT NULL,
`referee` varchar(35) COLLATE utf8_unicode_ci NOT NULL,
`referee_phone` varchar(16) COLLATE utf8_unicode_ci NOT NULL,
`hire_date` date NOT NULL,
`dt` date NOT NULL,
PRIMARY KEY (`employeeId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=7 ;
-- --------------------------------------------------------
--
-- Table structure for table `gender`
--
CREATE TABLE IF NOT EXISTS `gender` (
`genderId` int(11) NOT NULL AUTO_INCREMENT,
`gender` varchar(6) NOT NULL,
PRIMARY KEY (`genderId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `gender`
--
INSERT INTO `gender` (`genderId`, `gender`) VALUES
(1, 'Female'),
(2, 'Male');
-- --------------------------------------------------------
--
-- Table structure for table `image`
--
CREATE TABLE IF NOT EXISTS `image` (
`imageId` int(11) NOT NULL AUTO_INCREMENT,
`newname` varchar(255) NOT NULL,
PRIMARY KEY (`imageId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=57 ;