Page 1 of 1

INSERT query not working

Posted: Tue Apr 13, 2010 7:33 pm
by sirk
hi guys im not quite sure whats going wrong but my mysql insert query aint working, im trying to insert data for a job into my job table but it just keeps redirecting and until i added the error handler bit at the bottom it didnt say why, iv got a feeling its the query bit but cant see why iv checked over the variables and table names a few times but they seem right. the reason i think its the query is because it redirects me to the previous page with the error i set up if the query doesnt work anyway heres my code. thanks in advanced for any help

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" );
} 
$EmpID = $_SESSION['EmpID'];
include ('connect_db.php');

$JobID = $_POST['JobID'];
$UnitID = $_POST['UnitID'];
$OpndBy = $_POST['OpenedBy'];
$Assign = $_POST['AssignTo'];
$OpnDate = $_POST['OpenedDate'];
$WrkArea = $_POST['WorksArea'];
$Priority = $_POST['Priority'];
$Desc = $_POST['Description'];
$Comments = $_POST['Comments'];

if (!isset($JobID) || !isset($UnitID) || !isset($OpndBy) || !isset($Assign) || !isset($OpnDate) || !isset($WrkArea) || !isset($Priority) || !isset($Desc))
{
	$error = "Please fill out all of the starred fields";
	header( "Location:  AddNew.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";
	header( "Location:  AddNew.php?error=$error" );
}

if ($Priority==1)
{
	$TargetDate = date ('d/m/Y', strtotime('today +1 days'));
}
else if ($Priority==2)
{
	$TargetDate = date ('d/m/Y', strtotime('today +2 days'));
}
else if ($Priority==3)
{
	$TargetDate = date ('d/m/Y', strtotime('today +3 days'));
}
else
{
	$TargetDate = date ('d/m/Y', strtotime('today +4 days'));
}

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

if (!mysql_query($insert))
{
	$error = "Insert Failed";
	header( "Location:  AddNew.php?error=$error" );
}
else
{
	header ("location: Jobs.php");
}
?>

Re: INSERT query not working

Posted: Tue Apr 13, 2010 10:51 pm
by mrcoffee
I didn't thoroughly read your script, but I noticed that there's a comma missing after "Client_ClientID" in your insert query.

Re: INSERT query not working

Posted: Wed Apr 14, 2010 5:35 am
by sirk
Hey thanks for the reply, but unfortunatly that wasnt it it still doesnt work :(

Re: INSERT query not working

Posted: Wed Apr 14, 2010 6:04 am
by roders
add

Code: Select all

echo $insert
underneath the insert query. Where you have this

Code: Select all

$Desc = $_POST['Description'];
$Comments = $_POST['Comments'];
change it to

Code: Select all

$Desc = addslashes($_POST['Description']);
$Comments = addslashes($_POST['Comments']);

Re: INSERT query not working

Posted: Wed Apr 14, 2010 8:44 am
by sirk
problem solved i had stupidly set the database up wrong i put on delete do nothing instead of on delete set null etc same with on update

*edit* also thanks for that last bit of help :)