Page 1 of 2

Header Problem - dont understand PHP errors

Posted: Tue Aug 09, 2005 6:21 am
by mohson
Ok I have got this header problem.

When I add a record in my form and press submit the file processpeople.php is loaded - this file consists of an insert statement which works perfectly fine and inserts my data, BUT at the bottom of the file I have a line of code which basically links to a new blank form to allow another record to be added.

the problem is a new form wont load - instead I get a blank page. Error Reporting indicates two errors its more the second one that im concerned with though as the firs one isnt causing too many problems.

I know what the errors mean but dont know how to correct them because I cant see the problem.

Here are the errors
1 - PHP NOTICE - Undefined Variable: notes in processpeople.php at line 43

2- PHP WARNING - Cannot modify header information - headers already sent on line 52

Ok heres my code

Code: Select all

<?php

error_reporting(E_ALL);        // error reporting set to display all

/*Connecting, selecting database*/
$link = mysql_connect("vxxxxx", "xxxxx", "xxxxx")
   or die("Could not connect : " . mysql_error());
mysql_select_db("contact_management_system",$link) or die("Could not select database");

PRINT $sql = "INSERT INTO people 
   (
   person_id, salutation, firstname, surname, 
   organisation, role, address1, address2, 
   city, postcode, telephone, mobile, fax, 
   dateoflastcontact, datecontactagain, notes, email, 
   org_id, consultation_panel_member, primary_contact, primarycontactemail, advertising_grad_jobs,offer_mscproject,offer_ugproject,
	professional_devactivities,bcs_membership,bcs_pds,
	teaching_courses,academic_consultancy,employer_feedback
   ) 
 VALUES 
 
	(
	'$person_id','$salutation','$firstname','$surname', 
	'$organisation', '$role', '$address1', '$address2', 
	'$city', '$postcode', '$telephone', '$mobile', '$fax','$dateoflastcontact',
	'$datecontactagain','$notes','$email','$org_id',
	'$consultation_panel_member','$primary_contact',
	'$primarycontactemail','$advertising_grad_jobs',
	'$offer_mscproject','$offer_ugproject',
	'$professional_devactivities','$bcs_membership','$bcs_pds',
	'$teaching_courses','$academic_consultancy','$employer_feedback'
	)";

 $result = mysql_query($sql, $link) or die ( mysql_error($link));
 header("location: http://www.soi.city.ac.uk/organisation/ ... eople.html");
?>

Re: Header Problem - dont understand PHP errors

Posted: Tue Aug 09, 2005 6:34 am
by onion2k
mohson wrote:Here are the errors
1 - PHP NOTICE - Undefined Variable: notes in processpeople.php at line 43

2- PHP WARNING - Cannot modify header information - headers already sent on line 52
What's happening is that the first error is telling you that you're trying to access a variable that hasn't been set up yet. $notes is empty. You could get around that by using $_POST['notes'] instead (assuming you're posting the form).

The second error is telling you that it can't modify the headers sent to the user.. that's because there's already been some output. If you fix the first error, and remove bit thats printing $sql, that will go away and the form will work. Alternatively, move the header() function to the top of your script. But if you do that you'll never see any errors, even if the insert doesn't work.

Posted: Tue Aug 09, 2005 6:37 am
by mohson
with regards to the second point where is this 'other' output occurring?? I cant see it?? how do I locate it and remove it.

Posted: Tue Aug 09, 2005 6:42 am
by onion2k
mohson wrote:with regards to the second point where is this 'other' output occurring?? I cant see it?? how do I locate it and remove it.
The error message is the output. And you're printing your $sql variable. That's output too.
secondly if I use the post method you indicated would I have to do it for all the variables
It would be a good idea to. Coz you'll get that first error for any variables that aren't set.

Alternatively, you could just remove "error_reporting(E_ALL);" from the start of the script. But that's cheating.

Posted: Tue Aug 09, 2005 6:57 am
by mohson
Ive never really used post before Ive always wasted my time and put the values in twice like above - heres what im thinking of using now - what do you think?

Dont mind the variables for date they are for my date selector code - never showed you them before

Code: Select all

?php
 




/*Connecting, selecting database*/
$link = mysql_connect("xxx", "xxx", "0xx")
   or die("Could not connect : " . mysql_error());
mysql_select_db("contact_management_system",$link) or die("Could not select database");

$var1 = $_POST["dateoflastcontact_Year"];
$var2 = $_POST["dateoflastcontact_Day"];
$var3 = $_POST["dateoflastcontact_Month"];  
$dateoflastcontact = $var1."-".$var3."-".$var2;

$var1 = $_POST["datecontactagain_Year"];
$var2 = $_POST["datecontactagain_Day"];
$var3 = $_POST["datecontactagain_Month"];  
$datecontactagain = $var1."-".$var3."-".$var2;



   PRINT $sql = "INSERT INTO people 
   (
	$_POST['person_id'] , $_POST['salutation'] , $_POST['surname'] , 
	$_POST['organisation'] , $_POST['role'] , $_POST['role'] , $_POST['address1'] , $_POST['address2'] , $_POST['city'] , $_POST['postcode'] , $_POST['telephone'] ,
	$_POST['email'] , $_POST['org_id'] , $_POST['consultation_panel_member'] , $_POST['primary_contact'] , $_POST['primarycontactemail'] , $_POST['advertising_grad_jobs'] , $_POST['offer_mscproject'] , $_POST['offer_ugproject'] , $_POST['professional_devactivities'] , $_POST['bcs_membership'] , $_POST['bcs_pds'] , $_POST['teaching_courses'] ,
	$_POST['academic_consultancy'] , $_POST['employer_feedback'] 
	
	)";

$result = mysql_query($sql, $link) or die ( mysql_error($link));
 header("location: http://www.soi.city.ac.uk/organisation/ ... eople.html");
?>

Posted: Tue Aug 09, 2005 7:42 am
by shiznatix
you are still using print before your header, you can not do that. you can not output anything to the browser before the header you must send headers first

Posted: Tue Aug 09, 2005 8:27 am
by mohson
oh sorry - the thing is this problem occurred even before I put print in the code - infact the reason I put print there was so atleast the user sees something rather than a blank screen. What do you think of the new code should it work? what I mean is have I replaced the old code properly???

Posted: Tue Aug 09, 2005 8:49 am
by onion2k
You're not wrapping your variables in single quotes, so the SQL is very likely to go wrong if someone leaves a field blank..

Try this:

Code: Select all

$sql = "INSERT INTO people (";
$sql .= "'".$_POST['person_id']."',";
$sql .= "'".$_POST['salutation']."',";
$sql .= "'".$_POST['surname']."',";
$sql .= "'".$_POST['organisation']."',";
$sql .= "'".$_POST['role']."',";
$sql .= "'".$_POST['role']."',";
$sql .= "'".$_POST['address1']."',";
$sql .= "'".$_POST['address2']."',";
$sql .= "'".$_POST['city']."',";
$sql .= "'".$_POST['postcode']."',";
$sql .= "'".$_POST['telephone']."',";
$sql .= "'".$_POST['email']."',";
$sql .= "'".$_POST['org_id']."',";
$sql .= "'".$_POST['consultation_panel_member']."',";
$sql .= "'".$_POST['primary_contact']."',";
$sql .= "'".$_POST['primarycontactemail']."',";
$sql .= "'".$_POST['advertising_grad_jobs']."',";
$sql .= "'".$_POST['offer_mscproject']."',";
$sql .= "'".$_POST['offer_ugproject']."',";
$sql .= "'".$_POST['professional_devactivities']."',";
$sql .= "'".$_POST['bcs_membership']."',";
$sql .= "'".$_POST['bcs_pds']."',";
$sql .= "'".$_POST['teaching_courses']."',";
$sql .= "'".$_POST['academic_consultancy']."',";
$sql .= "'".$_POST['employer_feedback']."'";
$sql .= ")";

Posted: Tue Aug 09, 2005 9:35 am
by mohson
Thanks I tried that, I think there mite be something missing because I get an error message saying there is a error at line one??

any ideas what it could be???

Code: Select all

$sql = "INSERT INTO people (";
$sql .= "'".$_POST['person_id']."',";
$sql .= "'".$_POST['salutation']."',";
$sql .= "'".$_POST['surname']."',";
$sql .= "'".$_POST['organisation']."',";
$sql .= "'".$_POST['role']."',";
$sql .= "'".$_POST['role']."',";
$sql .= "'".$_POST['address1']."',";
$sql .= "'".$_POST['address2']."',";
$sql .= "'".$_POST['city']."',";
$sql .= "'".$_POST['postcode']."',";
$sql .= "'".$_POST['telephone']."',";
$sql .= "'".$_POST['email']."',";
$sql .= "'".$_POST['org_id']."',";
$sql .= "'".$_POST['consultation_panel_member']."',";
$sql .= "'".$_POST['primary_contact']."',";
$sql .= "'".$_POST['primarycontactemail']."',";
$sql .= "'".$_POST['advertising_grad_jobs']."',";
$sql .= "'".$_POST['offer_mscproject']."',";
$sql .= "'".$_POST['offer_ugproject']."',";
$sql .= "'".$_POST['professional_devactivities']."',";
$sql .= "'".$_POST['bcs_membership']."',";
$sql .= "'".$_POST['bcs_pds']."',";
$sql .= "'".$_POST['teaching_courses']."',";
$sql .= "'".$_POST['academic_consultancy']."',";
$sql .= "'".$_POST['employer_feedback']."'";
$sql .= ")"; 

 $result = mysql_query($sql, $link) or die ( mysql_error($link));
 header("location: http://www.soi.city.ac.uk/organisation/ ... eople.html");
?>

Posted: Tue Aug 09, 2005 9:40 am
by Grim...
What sort of error?
Can you copy and paste it?

Posted: Tue Aug 09, 2005 9:46 am
by kodersoftware
mohson wrote:Thanks I tried that, I think there mite be something missing because I get an error message saying there is a error at line one??

any ideas what it could be???

Code: Select all

$sql = "INSERT INTO people (";
$sql .= "'".$_POST['person_id']."',";
$sql .= "'".$_POST['salutation']."',";
$sql .= "'".$_POST['surname']."',";
$sql .= "'".$_POST['organisation']."',";
$sql .= "'".$_POST['role']."',";
$sql .= "'".$_POST['role']."',";
$sql .= "'".$_POST['address1']."',";
$sql .= "'".$_POST['address2']."',";
$sql .= "'".$_POST['city']."',";
$sql .= "'".$_POST['postcode']."',";
$sql .= "'".$_POST['telephone']."',";
$sql .= "'".$_POST['email']."',";
$sql .= "'".$_POST['org_id']."',";
$sql .= "'".$_POST['consultation_panel_member']."',";
$sql .= "'".$_POST['primary_contact']."',";
$sql .= "'".$_POST['primarycontactemail']."',";
$sql .= "'".$_POST['advertising_grad_jobs']."',";
$sql .= "'".$_POST['offer_mscproject']."',";
$sql .= "'".$_POST['offer_ugproject']."',";
$sql .= "'".$_POST['professional_devactivities']."',";
$sql .= "'".$_POST['bcs_membership']."',";
$sql .= "'".$_POST['bcs_pds']."',";
$sql .= "'".$_POST['teaching_courses']."',";
$sql .= "'".$_POST['academic_consultancy']."',";
$sql .= "'".$_POST['employer_feedback']."'";
$sql .= ")"; 

 $result = mysql_query($sql, $link) or die ( mysql_error($link));
 header("location: http://www.soi.city.ac.uk/organisation/ ... eople.html");
?>
i would reconstruct your insert statement to include the field names to rule out any errors with that. i.e.

Code: Select all

INSERT INTO suchnsuch (field1, field2, field3) values (val1, val2, val3)
and right after the final $sql .= ...
why don't you echo $sql; so we can see the resulting statement?

Posted: Tue Aug 09, 2005 9:48 am
by kodersoftware
and one more thing. for performance, you'd do better with something like:

Code: Select all

$sql = "INSERT INTO people (" .
          "'".$_POST['person_id']."'," .
          "'".$_POST['salutation']."'," .
          "'".$_POST['surname']."'," .
          "'".$_POST['organisation']."',"
...
...
...
not a big deal though. do it however. the code looks valid.

Posted: Tue Aug 09, 2005 9:57 am
by feyd
if you're going after performance, you'd use single quote strings only. :P

Posted: Tue Aug 09, 2005 10:08 am
by mohson
i would reconstruct your insert statement to include the field names to rule out any errors with that. i.e.

Code:
INSERT INTO suchnsuch (field1, field2, field3) values (val1, val2, val3)
The thing is I had done it this way from the beggining - look at the top of the post - I tried new method because I hoped it would prevent my header problem which is what this post is originally about.

I would rather just keep the

Code: Select all

$_POST[value]
Metod

Which should work but im getting this
You have an error in your SQL syntax near ''','Mr','','','','','','','','','','','','','','','','','','','','','','','')' at line 1

can this be fixed without reconstructing my Insert statement.


Also the most frustrating thing is that once I have changed my insert statement - which already worked - I will then still have to try and solve my header problem - one thing at a time hey!!

Heres my original code which worked perfectly fine but the header Problem was not allowing a blank form to load.

Code: Select all

<?php



/*Connecting, selecting database*/
$link = mysql_connect("xxx", "xxx", "xxxx")
   or die("Could not connect : " . mysql_error());
mysql_select_db("contact_management_system",$link) or die("Could not select database");

$var1 = $_POST["dateoflastcontact_Year"];
$var2 = $_POST["dateoflastcontact_Day"];
$var3 = $_POST["dateoflastcontact_Month"];  
$dateoflastcontact = $var1."-".$var3."-".$var2;

$var1 = $_POST["datecontactagain_Year"];
$var2 = $_POST["datecontactagain_Day"];
$var3 = $_POST["datecontactagain_Month"];  
$datecontactagain = $var1."-".$var3."-".$var2;



   $sql = "INSERT INTO people 
   (
   person_id, salutation, firstname, surname, 
   organisation, role, address1, address2, 
   city, postcode, telephone, mobile, fax, 
   dateoflastcontact, datecontactagain, notes, email, 
   org_id, consultation_panel_member, primary_contact, primarycontactemail, advertising_grad_jobs,offer_mscproject,offer_ugproject,
	professional_devactivities,bcs_membership,bcs_pds,
	teaching_courses,academic_consultancy,employer_feedback
   ) 
 VALUES 
 
	(
	'$person_id','$salutation','$firstname','$surname', 
	'$organisation', '$role', '$address1', '$address2', 
	'$city', '$postcode', '$telephone', '$mobile', '$fax','$dateoflastcontact',
	'$datecontactagain','$notes','$email','$org_id',
	'$consultation_panel_member','$primary_contact',
	'$primarycontactemail','$advertising_grad_jobs',
	'$offer_mscproject','$offer_ugproject',
	'$professional_devactivities','$bcs_membership','$bcs_pds',
	'$teaching_courses','$academic_consultancy','$employer_feedback'
	)";
	
	$result = mysql_query($sql, $link) or die ( mysql_error($link));;
	header("location: http://www.soi.city.ac.uk/organisation/ ... eople.html");
?>
As I said before the code inserted a record perfectly it was just that I was getting a blank sceen and not a blank new form.

The eerror Messages:

1 - PHP NOTICE - Undefined Variable: notes in processpeople.php at line 43

2- PHP WARNING - Cannot modify header information - headers already sent on line 52

Ok Now here is the code that It was suggested I try - which is a variation of what I had originally



Code: Select all

<?php
/*Connecting, selecting database*/
$link = mysql_connect("xxx", "xxx", "xxxx")
   or die("Could not connect : " . mysql_error());
mysql_select_db("contact_management_system",$link) or die("Could not select database");

$var1 = $_POST["dateoflastcontact_Year"];
$var2 = $_POST["dateoflastcontact_Day"];
$var3 = $_POST["dateoflastcontact_Month"];  
$dateoflastcontact = $var1."-".$var3."-".$var2;

$var1 = $_POST["datecontactagain_Year"];
$var2 = $_POST["datecontactagain_Day"];
$var3 = $_POST["datecontactagain_Month"];  
$datecontactagain = $var1."-".$var3."-".$var2;



    $sql = "INSERT INTO people (";
$sql .= "'".$_POST['person_id']."',";
$sql .= "'".$_POST['salutation']."',";
$sql .= "'".$_POST['surname']."',";
$sql .= "'".$_POST['organisation']."',";
$sql .= "'".$_POST['role']."',";
$sql .= "'".$_POST['role']."',";
$sql .= "'".$_POST['address1']."',";
$sql .= "'".$_POST['address2']."',";
$sql .= "'".$_POST['city']."',";
$sql .= "'".$_POST['postcode']."',";
$sql .= "'".$_POST['telephone']."',";
$sql .= "'".$_POST['email']."',";
$sql .= "'".$_POST['org_id']."',";
$sql .= "'".$_POST['consultation_panel_member']."',";
$sql .= "'".$_POST['primary_contact']."',";
$sql .= "'".$_POST['primarycontactemail']."',";
$sql .= "'".$_POST['advertising_grad_jobs']."',";
$sql .= "'".$_POST['offer_mscproject']."',";
$sql .= "'".$_POST['offer_ugproject']."',";
$sql .= "'".$_POST['professional_devactivities']."',";
$sql .= "'".$_POST['bcs_membership']."',";
$sql .= "'".$_POST['bcs_pds']."',";
$sql .= "'".$_POST['teaching_courses']."',";
$sql .= "'".$_POST['academic_consultancy']."',";
$sql .= "'".$_POST['employer_feedback']."'";
$sql .= ")"; 
	
	
	$result = mysql_query($sql, $link) or die ( mysql_error($link));;
	header("location: http://www.soi.city.ac.uk/organisation/ ... eople.html");
?>
and heres the error message for this code:
You have an error in your SQL syntax near ''','Mr','','','','','','','','','','','','','','','','','','','','','','','')' at line 1


Ok thanks for your patience -any ideas

Posted: Tue Aug 09, 2005 10:13 am
by feyd
echo out the query string so you can see what it's trying to send.