Problem with error catching

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sirk
Forum Newbie
Posts: 15
Joined: Sat Apr 03, 2010 8:40 am

Problem with error catching

Post by sirk »

Hello, basically im making a job management system, and part of its functionality is an update and insert option, for both of these iv set up if statements checking to see if the values that are posted from the form are either null or empty, and if they are to post a message alerting the user to this problem, but no matter if i leave the fields blank or if i only fill in a few or even fill them all in, i get the error that i set up that says "insert failed. empty fields" (Lines 93-94 in the code below) this is supposed to happen if for some reason the query fails, but the query is still executed and the table is populated, i checked this by looking in phpmyadmin, im not sure if its a sytax error or if im going about it the wrong way. if you have any advice or can spot whats wrong with my code all help is very much appreciated. thanks.

i also tried teh query in phpmyadmin just replacing teh variables with values.

here is the code for the insert its similar to the update only it runs a different sql query

Code: Select all

<?PHP  //error_reporting(E_ALL);
//start the session
session_start();
//check to make sure the session variable isn't registered
if(!session_is_registered('EmpID')){
//the session variable isn't registered, send them back to the login page
header( "Location: JMSystemLogin.php" );
} 
	else
	{
$EmpID = $_SESSION['EmpID'];
include ('connect_db.php');

$JobID = $_POST['JobID'];
$UnitID = $_POST['Unit'];
$OpndBy = $_POST['OpenedBy'];
$Assign = $_POST['AssignTo'];
$OpnDate = Date ("Y-m-d H:i:s");;
$WrkArea = $_POST['WorksArea'];
$Priority = $_POST['Priority'];
$Desc = addslashes($_POST['Description']);
$Comments = addslashes($_POST['Comments']);

		if ($JobID == NULL || $UnitID == NULL || $OpndBy == NULL || $Assign == NULL || $OpnDate == NULL || $WrkArea == NULL || $Priority == NULL || $Desc == NULL)
		{
			$error = "Please fill out all of the starred fields. Cant set fileds to a null value";
			header( "Location:  AddNew.php?error=$error" );
		}
			else 
			{
					if ($Priority==1)
					{
						$TargetDate = date ('Y-m-d H:i:s', strtotime('today +1 days'));
					}
						else if ($Priority==2)
						{
							$TargetDate = date ('Y-m-d H:i:s', strtotime('today +2 days'));
						}
						else if ($Priority==3)
						{
							$TargetDate = date ('Y-m-d H:i:s', strtotime('today +3 days'));
						}
						else
						{
							$TargetDate = date ('Y-m-d H:i:s', strtotime('today +4 days'));
						}

					$insert = "INSERT INTO Job (JobID, Client_ClientID, Employee_EmployeeID, Units_UnitID, JobDescription, JobOpendBy, Priority, DateSubmitted, TargetDate, Comments) VALUES 			('$JobID', '$WrkArea', '$EmpID', '$UnitID', '$Desc', '$OpndBy', '$Priority', '$OpnDate', '$TargetDate', '$Comments')";

					$result = mysql_query($insert);

					if (!$result)
					{
						$error = "Insert Failed. Null Values";
						header( "Location:  AddNew.php?error=$error" );
					}
						else
						{
							$error = "Insert Successful";
							header ("location: Jobs.php?error=$error");
						}
			}
 		if (empty($JobID) || empty($UnitID) || empty($OpndBy) || empty($Assign) || empty($OpnDate) || empty($WrkArea) || empty($Priority) || empty($Desc))
		{
			$error = "Please fill out all of the starred fields. Cant have any empty fields";
			header( "Location:  AddNew.php?error=$error" );
		}
			else
			{
				if ($Priority==1)
				{
				$TargetDate = date ('Y-m-d H:i:s', strtotime('today +1 days'));
				}
					else if ($Priority==2)
					{
						$TargetDate = date ('Y-m-d H:i:s', strtotime('today +2 days'));
					}
					else if ($Priority==3)
					{
						$TargetDate = date ('Y-m-d H:i:s', strtotime('today +3 days'));
					}
					else
					{
						$TargetDate = date ('Y-m-d H:i:s', strtotime('today +4 days'));
					}

				$insert = "INSERT INTO Job (JobID, Client_ClientID, Employee_EmployeeID, Units_UnitID, JobDescription, JobOpendBy, Priority, DateSubmitted, TargetDate, Comments) VALUES ('$JobID', '$WrkArea', '$EmpID', '$UnitID', '$Desc', '$OpndBy', '$Priority', '$OpnDate', '$TargetDate', '$Comments')";

				$result = mysql_query($insert);

				if (!$result)
				{
					$error = "Insert Failed. Empty Values";
					header( "Location:  AddNew.php?error=$error" );
				}
					else
					{
						$error = "Insert Successful";
						header ("location: Jobs.php?error=$error");
					}
		}
}

?>
User avatar
Sofw_Arch_Dev
Forum Commoner
Posts: 60
Joined: Tue Mar 16, 2010 4:06 pm
Location: San Francisco, California, US

Re: Problem with error catching

Post by Sofw_Arch_Dev »

If you're sure that the result of the mysql_query indicates an error, what do mysql_error() and mysql_errno() return?
Post Reply