Now, I have another problem about duplication validation of my data in db.. the message "...already exist" if function validate was through. now if I add a new data, same message pop up...and i can't add new data. I try to reverse my return value and I can add but the duplication validation was ignore.

here's my code:in mainController.php

function addDepartment(){
		$depart = new department();
		$departmentCode = $_POST['departmentCode'];
		$departmentDsc = $_POST['departmentDsc'];
		$check = $depart->validateDept($departmentCode,$departmentDsc);
		if ($check) {
			echo "<script language='javascript'>alert('Department Already Exist');
					window.location='department.php';</script>";		
		} else {
			$stat = $depart->addDepartment($departmentCode,$departmentDsc);
			if($stat){
				echo "<script language='javascript'>alert('Department successfully added');
					window.location='department.php';</script>";
			}else{
				echo "<script language='javascript'>alert('Error has occurred while adding the Department');
					window.location='department.php';</script>";		
			}	
		}
	}

and here's the function validateDept codes:

function validateDept($departmentCode,$departmentDsc) {
		$con = new connection();
		$query = "SELECT departID FROM tbldepart WHERE departCode='".$this->getDepartmentCode()."' ";
		$res = mysql_query($query);
			if(mysql_num_rows($res)>0){
				return false;	
			} else {
				return true;
			}
		}

i think you should have to change your 6th line to this:

if (!$check) {

I agree with Shanti Chepuru about the logic.

Also, not clear why you call $this->getDepartmentCode() in the query building statement when that information is being passed in as a parameter. Is that method returning the right piece of data?

Hi madCoder,

here's the whole code:

<?php
require_once('classConnection.php');
class department{
	protected $departmentID,$departmentCode, $departmentDsc;
	function __construct($id=0){
		$con = new connection();
		$query = "SELECT * FROM tbldepart WHERE departID='".$id."'";
		$res = mysql_query($query);
		if(mysql_num_rows($res)>0){
			$row = mysql_fetch_array($res);
			$this->setDepartmentID($row['departID']);
			$this->setDepartmentCode($row['departCode']);
			$this->setDepartmentDsc($row['departDsc']);
		}
		
	}
	
	function setDepartmentID($newDepartmentID){
		$this->departmentID = $newDepartmentID;
	}
	function setDepartmentCode($newDepartmentCode){
		$this->departmentCode = $newDepartmentCode;
	}
	function setDepartmentDsc($newDepartmentDsc){
		$this->departmentDsc=$newDepartmentDsc;
	}

	function getDepartmentID(){
		return $this->departmentID;
	}
	function getDepartmentCode(){
		return $this->departmentCode;
	}
	function getDepartmentDsc(){
		return $this->departmentDsc;
	}
	
	function validateDept($departmentCode,$departmentDsc) {
		$con = new connection();
		$query = "SELECT departID FROM tbldepart WHERE departCode='".$departmentCode."' AND departDsc = '".$departmentDsc."' ";
		$res = mysql_query($query);
			if(mysql_num_rows($res)>0){
				return TRUE;	
			} else {
				return FALSE;
			}
		}
	
	function showAllDepartment(){
		$con = new connection();
		$query = "SELECT departID FROM tbldepart ORDER BY departCode ASC";
		$res = mysql_query($query);
		if(mysql_num_rows($res)>0){
			while($row = mysql_fetch_array($res)){
				$theid[] = $row['departID'];
			}
			return $theid;
		}
		
	}
	function addDepartment($departmentCode,$departmentDsc){
		$con = new connection();
		$query = "INSERT INTO tbldepart SET departCode='".$departmentCode."',
				departDsc = '".$departmentDsc."' ";
		$res = mysql_query($query);
		if($res) return 1;
		else return 0;
	}
	function updateDepartment(){
		$con = new connection();
		$query = "UPDATE tbldepart SET departCode='".$this->getDepartmentCode()."', departDsc='".$this->getDepartmentDsc()."' 
				WHERE departID='".$this->getDepartmentID()."'";
		$res = mysql_query($query);
		if($res) return 1;
		else return 0;
	}
	function deleteDepartment(){
		$con = new connection();
		$query = "DELETE FROM tbldepart WHERE departID='".$this->getDepartmentID()."'";
		$res = mysql_query($query);
		if($res) return 1;
		else return 0;
	}
}
?>

I had solved my own problem...thanks guys for your help... i used the AND for that query and the duplication validation now works for me... thanks. God Bless...

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.