Help - Search - Members - Calendar
Full Version: Cannot modify header information
Weborum Webmaster Forum > Web Page Design > PHP
Joe
For some reason I'm getting this error online whereas all my tests on my home server never brought up a single problem ...

CODE
Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/joe2kiss/public_html/joe2torials2/CMS/updatesubjectrecord.php on line 22

Warning: Cannot modify header information - headers already sent by (output started at /home/joe2kiss/public_html/joe2torials2/CMS/updatesubjectrecord.php:10) in /home/joe2kiss/public_html/joe2torials2/CMS/updatesubjectrecord.php on line 23


My code;

PHP

<?php
session_start
();

if(&
#33;isset($_SESSION['loggedin'])) {
   
header('Location: '.$domain.'index.php?error=1');
   exit();
}
?>

<?php
include 'config.php';

$ID = $_SESSION['ID'];
$subject = $_POST['subject'];

$subject = safeAddSlashes($subject);

$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);
$requete = "UPDATE subject SET subject = '$subject' WHERE ID = '$ID'";
$result = mysql_query ($requete,$db);
mysql_free_result($result);
header('Location: '.$domain.'edittutorialsub.php');
exit();

?>



Lines 22 and 23 are;

mysql_free_result($result);
header('Location: '.$domain.'edittutorialsub.php');

Headers are already sent out ... but they're not blink.gif

Can anyone see what I'm doing wrong here?

TIA
Timo
What's in the config.php? Just variables?

Just to test it...
Try removing

if(!isset($_SESSION['loggedin'])) {
header('Location: '.$domain.'index.php?error=1');
exit();
}

and see if it gives the error.. I know it shouldn't affect it but still just curious.
-Tim
Joe
Config.php contains all my functions and database connection details.

ok Time, will see what it does a sec.
Joe
Nope, same error.

This time I got the error on line 16 which was;

header('Location: '.$domain.'edittutorialsub.php');
sjthomas
Your getting the header error I suspect because of the connection error for MySQL is outputting to the browser, hence its too late to send headers cause something already been sent (I think the ehader re-direct the error is referring to is on line 24, the line directly above it is causing the error). Double check your connection settings in your config file, I always mess up because my host uses a prefix for MySQL username and DB's which I always forget to add.

One thing though, and I'm no guru on SQL but is the table name the same as the field name in the line:

QUOTE
PHP
$requete = \"UPDATE subject SET subject = '$subject' WHERE ID = '$ID'\";


Cause usually its UPDATE nameofthetable SET fieldname = '$subject' ......
Joe
Sorry I took so long to reply to this. Yup Si, the sql query was correct smile.gif

I've started fresh on the CMS now and still getting the error, on the login form this time pinch.gif

And I can't see whats wrong with it because I used the exact same code before and it worked.

http://www.joe2torials.com/cms/index.php

My code;

PHP
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>

    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta name="author" content="Joseph Skidmore" />
    <meta name="Copyright" content="Copyright (c) 2005 Joseph Skidmore" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <meta name="MSSmartTagsPreventParsing" content="true" />

    <title>Joe2Torials Content Management System Login</title>
    
    <link href="index.css" rel="stylesheet" type="text/css" />

</head>
<body>

<div id="wrapper">
    <div id="loginform">

<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']?>">
<p><label for="username"><strong>Username :</strong><br />
<input type="text" name="username" size="30" id="username" /></label></p>

<p><label for="password"><strong>Password :</strong><br />
<input type="password" name="password" size="30" id="password" /></label></p>

<p><input type="submit" name="submit" value="Submit" class="button" /> <input type="reset" name="reset" value="Reset" class="button" /></p>
</form>

<?
if(isset($_POST['submit'])) {

include
'config.php';

$username = $_POST['username'];
$password = $_POST['password'];

$username = Encrypt($username);
$password = Encrypt($password);

$username = safeAddSlashes($username);
$password = safeAddSlashes($password);

$db = mysql_connect($dbHost,$dbUser,$dbPass);
mysql_select_db($dbname,$db);
$query = "SELECT user, pass FROM login WHERE user = '$username' AND pass = '$password'";
$result = mysql_query($query, $db) or die(mysql_error());

if(
mysql_num_rows($result)) {
    
$_SESSION['loggedin'] = 1;
    
header('Location: '.$domain.'admin.php');
    exit();
}
else {
    
header('Location: '.$domain.'index.php?error=1');
exit();
}

}
?>
    </div>
</div>

</body>
</html>


And line 58 where I'm getting the error is;
header('Location: '.$domain.'index.php?error=1');

I can't see what the problem is because I've used the exact same code before and it all worked fine.
chipgraphics
[quote=joe2kiss,Mar 3 2005, 07:46 AM]
Sorry I took so long to reply to this. Yup Si, the sql query was correct smile.gif

I've started fresh on the CMS now and still getting the error, on the login form this time pinch.gif

And I can't see whats wrong with it because I used the exact same code before and it worked.

http://www.joe2torials.com/cms/index.php

My code;

PHP
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>

    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta name="author" content="Joseph Skidmore" />
    <meta name="Copyright" content="Copyright (c) 2005 Joseph Skidmore" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <meta name="MSSmartTagsPreventParsing" content="true" />

    <title>Joe2Torials Content Management System Login</title>
    
    <link href="index.css" rel="stylesheet" type="text/css" />

</head>
<body>

<div id="wrapper">
    <div id="loginform">

<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']?>">
<p><label for="username"><strong>Username :</strong><br />
<input type="text" name="username" size="30" id="username" /></label></p>

<p><label for="password"><strong>Password :</strong><br />
<input type="password" name="password" size="30" id="password" /></label></p>

<p><input type="submit" name="submit" value="Submit" class="button" /> <input type="reset" name="reset" value="Reset" class="button" /></p>
</form>
<?php

/* ---v---------v--------------------v--------------------  move to top of page ------ */
?>
<?
if(isset($_POST['submit'])) {

include
'config.php';

$username = $_POST['username'];
$password = $_POST['password'];

$username = Encrypt($username);
$password = Encrypt($password);

$username = safeAddSlashes($username);
$password = safeAddSlashes($password);

$db = mysql_connect($dbHost,$dbUser,$dbPass);
mysql_select_db($dbname,$db);
$query = "SELECT user, pass FROM login WHERE user = '$username' AND pass = '$password'";
$result = mysql_query($query, $db) or die(mysql_error());

if(
mysql_num_rows($result)) {
    
$_SESSION['loggedin'] = 1;
    
header('Location: '.$domain.'admin.php');
    exit();
}
else {
    
header('Location: '.$domain.'index.php?error=1'); /* <------------- cannot be sent once you have sent any HTML headers. */
exit();
}

}
?>
    </div>
</div>

</body>
</html>



--------------------- Try this instead to work perfectly -------------------------------
PHP

<?php
session_start
();

if(isset(
$_POST['submit'])) {

include
'config.php';

$username = $_POST['username'];
$password = $_POST['password'];

$username = Encrypt($username); /* <--- smart move */
$password = Encrypt($password); /* <--- smart move */

$username = safeAddSlashes($username); /*  <-- would recommend base64_encode for easy transport. */
$password = safeAddSlashes($password);/*  <-- would recommend base64_encode for easy transport. */

$db = mysql_connect($dbHost,$dbUser,$dbPass);
mysql_select_db($dbname,$db);
$query = "SELECT user, pass FROM login WHERE user = '$username' AND pass = '$password'";
$result = mysql_query($query, $db) or die(mysql_error());

/* ---------  Recommend alternate solution below -------------
if(mysql_num_rows($result)) {
    $_SESSION['loggedin'] = 1;  //  <---- careful - That's a session hijacker's dream (See below).  
    header('Location: '.$domain.'admin.php');
    exit();
}
else {
    header('Location: '.$domain.'index.php?error=1');  // <----now if query succeeds send header.
exit();
}

}
*/
/* -------------------------------------------------------------------------------- */
if(mysql_num_rows($result) > 0) {

   
$_SESSION['loggedin'] = Encrypt($something);

   
$redirect_admin = $domain."admin.php";
   
header(sprintf("Location: %s", $redirect_admin));
    exit();

} else {

   
$_SESSION['error'] = "Invalid Username or Password";
  
/*  this way you can show an error and it will just re-display this page. */

}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>

    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta name="author" content="Joseph Skidmore" />
    <meta name="Copyright" content="Copyright (c) 2005 Joseph Skidmore" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <meta name="MSSmartTagsPreventParsing" content="true" />

    <title>Joe2Torials Content Management System Login</title>
    
    <link href="index.css" rel="stylesheet" type="text/css" />

</head>
<body>

<div id="wrapper">
    <div id="loginform">
<?php /*  Display error message if invalid login */ ?>
<center><?php if(isset($_SESSION['error'])) { echo $_SESSION['error']; } ?></center>
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<p><label for="username"><strong>Username :</strong><br />
<input type="text" name="username" size="30" id="username" /></label></p>

<p><label for="password"><strong>Password :</strong><br />
<input type="password" name="password" size="30" id="password" /></label></p>

<p><input type="submit" name="submit" value="Submit" class="button" /> <input type="reset" name="reset" value="Reset" class="button" /></p>
</form>

</div>
</div>

</body>
</html>


You have the right idea. Just in the wrong spot.

good luck

Chipgraphics
www.chipgraphics.net
Joe
Thanks mate biggrin.gif specool.gif

I did however find an easier solution by using ob_start() and ob_end_flush().
aviansuicide
QUOTE(joe2kiss @ Mar 11 2005, 04:22 PM)
I did however find an easier solution by using ob_start() and ob_end_flush().
[right][snapback]24535[/snapback][/right]


*cough* I wonder how wink.gif
Joe
laugh.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.