Okay,i try to call a function of mysql_prep() that i made in functions.php.
I try to call this function at the page of create_subjects.php by typing this

<?php require_once("includes/functions.php");

into the create_subjects.php.

and this is the full code of create_subjects.php :

<?php require_once("includes/functions.php");
?>
<?php
$connection = mysql_connect("localhost","root","mypassword");
if (!$connection) {
  die ("database failed to connect " . mysql_error());
}

$db_select = mysql_select_db("newcompany");
if (!$db_select) {
  die ("database failed to connect " . mysql_error());
}


// a page ridirected using header : cnnt contain spaces or anything between PHP tag
	$menu_name = mysql_prep($_POST['menu_name']);
	$position = mysql_prep($_POST['position']);
	$visible = mysql_prep($_POST['visible']);

	$query = "INSERT INTO subjects (
				menu_name, position, visible
			) VALUES (
				'{$menu_name}', {$position}, {$visible}
			)";         // not neccessarily nd to put { symbol when inserting values.
	$result = mysql_query($query, $connection);
	if ($result) {
		// Success!
                header("Location: content.php");
		exit;

	} else {
		// Display error message.
		echo "<p>Subject creation failed.</p>";
		echo "<p>" . mysql_error() . "</p>";
	}


mysql_close($connection);
?>

This is the error that i got from my browser when i try to create a new page :
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\newcompany\includes\functions.php:50) in C:\xampp\htdocs\newcompany\create_subjects.php on line 28

and this is the full code of functions.php :

<?php

function mysql_prep( $value ) {
		$magic_quotes_active = get_magic_quotes_gpc();
		$new_enough_php = function_exists( "mysql_real_escape_string" ); // i.e. PHP >= v4.3.0 ,this func oli exist 4 PHP above v4.3.0
		if( $new_enough_php ) { // PHP v4.3.0 or higher
			// undo any magic quote effects so mysql_real_escape_string can do the work
			if( $magic_quotes_active ) { $value = stripslashes( $value ); }
			$value = mysql_real_escape_string( $value );
		} else { // before PHP v4.3.0
			// if magic quotes aren't already on then add slashes manually
			if( !$magic_quotes_active ) { $value = addslashes( $value ); }
			// if magic quotes are active, then the slashes already exist
		}
		return $value;
	}

function get_all_subjects() {
     global $connection;
    $subject_set = mysql_query("SELECT * FROM subjects", $connection);
	if (!$subject_set) {
                die ("database failed to connect " . mysql_error());
        }
		return $subject_set;
}

function get_pages_for_subject($subject_id) {
     global $connection;
    $page_set = mysql_query("SELECT * FROM pages WHERE subject_id = {$subject_id}");
        if (!$page_set) {
                die ("x leh connect!!!! : " . mysql_error());
         }
		return $page_set;
}
?>

help please..
Thank You...

Recommended Answers

All 9 Replies

Haha.. I just found this kinda funny cause I saw this issue by someone else earlier today. I posted the cause and solution here:

http://www.daniweb.com/forums/thread286818.html

Simply put, you have white space (the new lines outside of the closing and opening PHP tags) which is causing output data to be sent. This breaks headers, so you need to remove that white space.

yeah..but if you look carefully onto my codes,theres no such things like white spaces or what..

it is just when i inserted the functions.php code,the browser came out with error..

I really appreciate if you could just go through my codes and figure out whats wrong..

Thank You...

commented: too prideful to admit they didn't understand something, doesn't like to admit they needed help for something simple +0

huh..i dont know where is wrong,but suddenly just know when i was playing with the codes,my problem is fixed..

my head is really upside down right now..huh..

Howdy. First off, glad you got it fixed.

And I think you are mistaken with what whitespace refers to .. it doesn't just mean spaces, but also carriage returns, tabs and new lines.

For example, here was some code which I referred to:

?>
<?php

.. from create_subjects.php

The new line after the closing PHP tag is white space which gets output before header gets set.

so if it is like that..

i only allow to write that code in this way each time i want to close and want to begin the new PHP tag :

?><?php

instead of

?>
<?php

is that what you are trying to say?

Exactly...

It sounds stupid - I know.. But what actually goes on there is that by having the closing and opening PHP tags on new lines, this is sending HTML output to the browser (a new line character), and a new line character is the same as spacing or anythiing else and will break the header() function.

If you plan to write code like that, you can also try putting ob_clean(); right before any header() calls, and it should clear out any data that has been output already.

why close open php at all? it just adds execution time, doesnt make the code easier to read, no care has been taken with clean code

It is quite enough to put a new line (or space) before 1st <?php to get this error
or add a newline after last ?>
The prob is -- that is quite invisible (especially new line after last ?>)

Most likelly you have fixed the issue by letting your IDE to format the code -- most of IDEs remove those new lines

UPD:
should any of your functions from functions.php throw a warning or output any error message -- you will face the same problem with headers.
Use error_reporting() and @ (but very carefully and after thorough oce testing)

Anybody can help me with this code? My varification cannot sent a mail to email :( pls help me
<?
include('config.php');

// table name
$tbl_name=temp_members_db;

// Random confirmation code
$confirm_code=md5(uniqid(rand()));

// values sent from form
$name=$_POST;
$email=$_POST;
$country=$_POST;

// Insert data into database
$sql="INSERT INTO $tbl_name(confirm_code, name, email, password, country)VALUES('$confirm_code', '$name', '$email', '$password', '$country')";
$result=mysql_query($sql);

// if suceesfully inserted data into database, send confirmation link to email
if($result){

// ---------------- SEND MAIL FORM ----------------

// send e-mail to ...
$to=$email;

// Your subject
$subject="Your confirmation link here";

// From
$header="from: your name <your email>";

// Your message
$message="Your Comfirmation link \r\n";
$message.="Click on this link to activate your account \r\n";
$message.="http://www.yourweb.com/confirmation.php?passkey=$confirm_code";

// send email
$sentmail = mail($to,$subject,$message,$header);

}

// if not found
else {
echo "Not found your email in our database";
}

// if your email succesfully sent
if($sentmail){
echo "Your Confirmation link Has Been Sent To Your Email Address.";
}
else {
echo "Cannot send Confirmation link to your e-mail address";
}

?>

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.