session is 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
Spectrum_tr
Forum Newbie
Posts: 14
Joined: Sat Dec 10, 2005 11:22 am

session is not working

Post by Spectrum_tr »

Hi again :) Now, i have trouble with the session.

I have to use a variable (exists in existing page ) in the next page and i learnt that it can be done by sessions.

Code: Select all

session_start();
$_SESSION['department_id']=$theRow[0];
im doing this in the first page.

and in the next page:

Code: Select all

session_start();
$old_dept_ID=$_SESSION['department_id'];
and it is giving me these errors(in both 2 pages) :

Code: Select all

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at c:\wamp\www\update_d.php:4) in c:\wamp\www\update_d.php on line 16

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at c:\wamp\www\update_d.php:4) in c:\wamp\www\update_d.php on line 16
when i delete the session_start(); and try to see whats inside of $old_dept_ID, nothing is seen. The variable is null. How can i use sessions to store the "$theRow[0]" variable??
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at c:\wamp\www\update_d.php:4) in c:\wamp\www\update_d.php on line 16
see where it says output started at blah? session start must be called before any output is sent. try moving it to the very top of your code :-D
Spectrum_tr
Forum Newbie
Posts: 14
Joined: Sat Dec 10, 2005 11:22 am

Post by Spectrum_tr »

Thank you. Yes i did that. Its not giving me any error now, but again i can not use the session.

I can not get my variable $old_dept_ID=$_SESSION['department_id'];

Its not working.

My first page of the code is:

Code: Select all

<?php

session_start();
$_SESSION['department_id']=$theRow[0];

$link = mysql_connect('localhost', 'root')
   or die('Could not connect: ' . mysql_error());
mysql_select_db('arda') or die('Could not select database');

$id = mysql_real_escape_string($_GET['id']);
$sql = "select * from departments where DEPARTMENT_ID = $id";

$result = mysql_query($sql);

echo " <P><STRONG>Departments Table </STRONG></P> ";
echo " <form action=\"update_dd.php\" method=\"POST\"> ";
echo " <TABLE width=700 border=1> ";
  echo " <TR> ";
    echo " <TD width=80><STRONG>DEPARTMENT_ID</STRONG></TD> ";
    echo " <TD width=80><STRONG>DEPARTMENT_NAME</STRONG></TD> ";
    echo " <TD width=80><STRONG>MANAGER_ID</STRONG></TD> ";
    echo " <TD width=80><STRONG>LOCATION_ID</STRONG></TD> ";
  echo " </TR> ";
 
  $theRow = mysql_fetch_row($result);

  echo " <TR> "; 
    echo " <TD><input type=\"text\" name=\"dept_id\" value=\"$theRow[0]\"/></TD> ";
    echo " <TD><input type=\"text\" name=\"dept_name\" value=\"$theRow[1]\"/></TD> ";
    echo " <TD><input type=\"text\" name=\"man_id\" value=\"$theRow[2]\"/></TD> ";
    echo " <TD><input type=\"text\" name=\"loc_id\" value=\"$theRow[3]\"/></TD> ";
  echo " </TR>";

echo " </TABLE> ";

echo " <input type=\"submit\" value=\"Submit\"> ";
echo " </form> ";

?>

<html>
<body>

</body>
</html>
And the second page is:

Code: Select all

<?php

session_start();
$old_dept_ID=$_SESSION['department_id'];
	
$link = mysql_connect('localhost', 'root') or die('Could not connect: ' . mysql_error());
mysql_select_db('arda') or die('Could not select database');

$dept_ID = $_POST['dept_id'];
$dept_NAME = $_POST['dept_name'];
$man_ID = $_POST['man_id'];
$loc_ID = $_POST['loc_id'];

$updateQuery = " update departments set department_id='$dept_ID', department_name='$dept_NAME', 
				        manager_id='$man_ID', location_id='$loc_ID' where department_id='$old_dept_ID' ";

$done = mysql_query($updateQuery);

if ($done)
	echo "Updating is successful";
else
        echo "Updating is not successful, mysqlerror()";
	
?>	

<html>
<body>

<br>
<a href="indexx.php">Click here to return to the main page</a>

</body>
</html>
??????? What can i do?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

well you are not setting the session to anything, since you have the session = therow thing at the very top, $therow[0] is nothing yet, therefor it gets set to nothing. you can set the sessions whenever you like in a script, its just the session_start() that has to go at the very top of the page.
Spectrum_tr
Forum Newbie
Posts: 14
Joined: Sat Dec 10, 2005 11:22 am

Post by Spectrum_tr »

Yeah i got it now. It was a stupid mistake as you notice but im a beginner in this language (and also all the languages :)) so im doing this kind of mistakes. Thank you so much...
Post Reply