SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    session is not working

    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.

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

    and in the next page:

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

    Code:
    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??

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2005
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're starting your sessions after you've already sent some HTML to the browser. Do your session stuff at the top of page before anything is outputted.

  3. #3
    SitePoint Enthusiast cuongdm's Avatar
    Join Date
    Feb 2005
    Location
    www.VietBright.com
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try at the most simple first and then, add part by part later and make sure everything still work well... in your case, just create 2 php pages with those code only and test...
    visit my site [ Vietnam web design company ]

  4. #4
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you. I put them at the top. 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:

    PHP Code:
    <?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:

    PHP Code:
    <?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?

  5. #5
    SitePoint Zealot
    Join Date
    Aug 2005
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your first page is ordered kind of weird. Right now, you're assigning an empty variable to your session variable.

    Try something like this. It's your same script but reordered slightly.

    Database first, session second, then all your output at the end.

    PHP Code:
    <?php
    $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);
    $theRow mysql_fetch_row($result);

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

    <html>
    <body>

    <?php
    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> ";
      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> ";
    ?>

    </body>
    </html>

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,494
    Mentioned
    164 Post(s)
    Tagged
    1 Thread(s)

    got IE?

    If you're using IE6 try this
    PHP Code:
    <?php
    session_start
    ();
    header("Cache-control: private"); //IE 6 Fix

  7. #7
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok i got it. Thank you all so much for helping me!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •