Ok so I have been working on this all day but cant get it.
I want to add an Employee ID to an Employee Table....I got this much...But first I need to check if that ID exists already. How can I do this, here is my code:

<?php
session_start();
require_once 'php_includes.php';

//Make sure logged in
checkLogin();
	
///////////////////////////////////
//This is only for manager only pages. Verifies that the person viewing this page is a manager.
sqlConnect();
$con=sqlConnect();
$sql = 'select ManagerID from Employee where EmployeeID = ' . $_SESSION['tmsUserID']; 
	
// Perform Query
$sqlresult = sqlQuery($sql);

$result = mysql_result($sqlresult, 0, 0);

if( $_SESSION['tmsUserID'] != $result )
{
	$_SESSION['errorMessage'] = "Access denied";
	include("error.php");
	exit();
}
////////////////////////
else
{
	//Makes sure that the userId and Session User Id are not the same because only managers have same user ID and same ManagerID
	if ($_SESSION['tmsUserID'] == $_POST['userid'])
	{
		exit("<p>Illegal Action! Please choose a different Employee ID <br> Click <a href=\"addemployee.php\">here</a> to go back.</p>");
	}
	else
	{
//Checks if the user has entered values in all fields
if(empty($_POST['userid']) ||empty($_POST['password']))
{
exit("<p>You must enter values in all fields of the Add Employee form! <br> Click <a href=\"addemployee.php\">here</a> to go back.</p>");
}
///////////////
/////THIS IS WHERE I NEED HELP!!!! after this else I need to check if the employeeID exists or not. IF it does then ask the user to go back, if it does not exists then perform $sql2 query.
//////////////////////////////////////////////////
else
{
mysql_select_db("titans", $con);	

//adds employee to database
$sql2="INSERT INTO Employee (employeeID, employeePassword, ManagerID)
VALUES
('$_POST[userid]','$_POST[password]','$result')";

if(!sqlQuery($sql2))
{
include("error.php");
exit();
}
else
{
echo "Employee added.<br> <p> Click <a href=\"addemployee.php\">here</a> to add another employee.</p> ";

//Close DB
sqlExit();
}
}
}
}
?>

HELP!!!

Recommended Answers

All 10 Replies

You must keep id column unique at database level. Without checking the duplication at frontend, you must attempt to insert a new row. If id exist in database it will throw an error with error code which you should trace in your code. if error code is for duplicate id then display appropropriate message and allow user to change id.

Ok so I have been working on this all day but cant get it.
I want to add an Employee ID to an Employee Table....I got this much...But first I need to check if that ID exists already. How can I do this, here is my code:

<?php
session_start();
require_once 'php_includes.php';

//Make sure logged in
checkLogin();
	
///////////////////////////////////
//This is only for manager only pages. Verifies that the person viewing this page is a manager.
sqlConnect();
$con=sqlConnect();
$sql = 'select ManagerID from Employee where EmployeeID = ' . $_SESSION['tmsUserID']; 
	
// Perform Query
$sqlresult = sqlQuery($sql);

$result = mysql_result($sqlresult, 0, 0);

if( $_SESSION['tmsUserID'] != $result )
{
	$_SESSION['errorMessage'] = "Access denied";
	include("error.php");
	exit();
}
////////////////////////
else
{
	//Makes sure that the userId and Session User Id are not the same because only managers have same user ID and same ManagerID
	if ($_SESSION['tmsUserID'] == $_POST['userid'])
	{
		exit("<p>Illegal Action! Please choose a different Employee ID <br> Click <a href=\"addemployee.php\">here</a> to go back.</p>");
	}
	else
	{
//Checks if the user has entered values in all fields
if(empty($_POST['userid']) ||empty($_POST['password']))
{
exit("<p>You must enter values in all fields of the Add Employee form! <br> Click <a href=\"addemployee.php\">here</a> to go back.</p>");
}
///////////////
/////THIS IS WHERE I NEED HELP!!!! after this else I need to check if the employeeID exists or not. IF it does then ask the user to go back, if it does not exists then perform $sql2 query.
//////////////////////////////////////////////////
else
{
mysql_select_db("titans", $con);	

//adds employee to database
$sql2="INSERT INTO Employee (employeeID, employeePassword, ManagerID)
VALUES
('$_POST[userid]','$_POST[password]','$result')";

if(!sqlQuery($sql2))
{
include("error.php");
exit();
}
else
{
echo "Employee added.<br> <p> Click <a href=\"addemployee.php\">here</a> to add another employee.</p> ";

//Close DB
sqlExit();
}
}
}
}
?>

HELP!!!

before trying to add the user to ur DB execute a query that will try to retrieve the existing users with same id.. something like..

$query = "SELECT count(employeeID) FROM Employee
              WHERE employeeID = '$_POST[userid]';

Then check if count comes out to be greater than 0, u can redirect the user back to the form..
Hope this helps
Cheers!!

<?

$query = "SELECT employeeID FROM Employee

WHERE employeeID = '$_POST[userid]';
$count= mysql_num_rows($query);
if($count==1)
{
echo "id alreadyexists";
}
else
{
//ur code
}
?>
<?

$query = "SELECT employeeID FROM Employee

WHERE employeeID = '$_POST[userid]';
$count= mysql_num_rows($query);
if($count==1)
{
echo "id alreadyexists";
}
else
{
//ur code
}
?>

So far so good... another suggestion would be to use the same with ajax... It gives ur application a professional look and the user doesn't get annoyed if the id he chose already exists because he never loses the information he entered in the form...
check this link out...

Member Avatar for nileshgr

MySQL and PostgreSQL both support automatic id field. Infact postgresql allows you to specify a specific sequence (Arithmetic Progressions). i.e. you can define the start id, increment value, and end value (it can infinity too).

Use these db features and don't make your app do this.

MySQL and PostgreSQL both support automatic id field. Infact postgresql allows you to specify a specific sequence (Arithmetic Progressions). i.e. you can define the start id, increment value, and end value (it can infinity too).

Use these db features and don't make your app do this.

itech, ur suggestion is worth consideration but look at his requirement, he is using this id for login... not necessarily be a numeric value to uniquely identify an entry... :)
i dont blame u.. the variable name he used is the source of confusion... it shud have been loginId or username..

Member Avatar for nileshgr

itech, ur suggestion is worth consideration but look at his requirement, he is using this id for login... not necessarily be a numeric value to uniquely identify an entry... :)
i dont blame u.. the variable name he used is the source of confusion... it shud have been loginId or username..

yeah if he uses pgsql, then he can use empid number for login (that won't be user-friendly, but still many companies have that).

Thank you guys, I actually got it to work last night. I kept on searching and I found it.

Thank you guys, I actually got it to work last night. I kept on searching and I found it.

Well in that case you should mark the thread as solved so people wont bother trying to solve ur problem... :)

Well in that case you should mark the thread as solved so people wont bother trying to solve ur problem... :)

ahhh sorry i didnt noticed u already did so... :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.