INSERT query not working

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

INSERT query not working

Post 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");
}
?>
mrcoffee
Forum Commoner
Posts: 31
Joined: Tue Nov 10, 2009 3:03 pm
Location: Wyoming, USA

Re: INSERT query not working

Post 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.
sirk
Forum Newbie
Posts: 15
Joined: Sat Apr 03, 2010 8:40 am

Re: INSERT query not working

Post by sirk »

Hey thanks for the reply, but unfortunatly that wasnt it it still doesnt work :(
roders
Forum Commoner
Posts: 68
Joined: Tue Oct 20, 2009 9:29 am

Re: INSERT query not working

Post 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']);
sirk
Forum Newbie
Posts: 15
Joined: Sat Apr 03, 2010 8:40 am

Re: INSERT query not working

Post 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 :)
Post Reply