Page 1 of 1

I Can`t uderstand how sessios work NEED HELP

Posted: Mon Apr 28, 2008 1:02 pm
by junelis
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hy, this is my first post on this forum i am begginer with sessions and i have some dificults. I will paste some of my code.
Every page where I use sessions i start with

Code: Select all

<? session_start()?>
The two pages from where I choose values have code like this :

Code: Select all

<?  include 'db.inc';
    
    //connect to your database 
      if (!($connection = @ mysql_connect($hostName,$username,$password))) 
         die("Could not connect to database");
      //specify database
      if (!mysql_select_db($databaseName, $connection))
         die("Unable to select database");
 
      // Build SQL Query  
      $query = "select * from itworker";
                
      $numresults=mysql_query($query);
      $numrows=mysql_num_rows($numresults);
      
      // If we have no results, offer a google search as an alternative
 
      if ($numrows == 0)
      {
       echo "<h4>Empty Table</h4>";
      }
      
      $result = mysql_query($query) or die("Couldn't execute query");
      $count = 1 ;
      // now you can display the results returned
        while ($row= mysql_fetch_array($result)) 
        {
            $idworker = $row["workerID"];
            $nameworker = $row["name"];
        $lastnameworker = $row["lastname"];
                                    
        echo "<a href=\"nalog.php?identworker=$idworker&imeeworker=$nameworker\">$count</a>";
            echo ".)&nbsp;$nameworker&nbsp;$lastnameworker<br>" ;   
                            
            $count++;
        }   ?>

when I choose value from the first page everything is OK my choosen values are shown in my main page but when i go to the seccond page and choose value from the seccond page and return to the main page the values from the first page are missing only the values from the seccond are shown. I have made some test and I understand that in my session file the values from my first page are deleted. I made sessions variables in my main page like this :

Code: Select all

// sessijni promenlivi koi se od stranata worker.php 
    $_SESSION['idworker'] = $identworker; // values from first page
    $_SESSION['imeworker'] = $imeeworker; //first page
    // sessijni promenlivi koi se od stranata searchuser.php 
    $_SESSION['iduser'] = $siduser; // values from second page
    $_SESSION['ime'] = $sime; // second page
    $_SESSION['prezime'] = $sprezime; // second page
    $_SESSION['telefon'] = $stelefon; // second page
    $_SESSION['mobilen'] = $smobilen; // second page
    $_SESSION['ulica'] = $sulica; // second page



Am i deleting session variales when i send values with href. And if I delete can you tall me how to remeber session variables directly in first page and second page when i have mysql_fetch_array($result).


Thanks !!!


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: I Can`t uderstand how sessios work NEED HELP

Posted: Mon Apr 28, 2008 2:21 pm
by pickle
I'm not really sure what your situation is - could you try to explain it again?

Sessions are not tied to $_GET parameters at all - you can't delete/overwrite session variables by putting something in your URL.

Re: I Can`t uderstand how sessios work NEED HELP

Posted: Mon Apr 28, 2008 2:44 pm
by junelis
The problem is that i define session variables in my main page because from the first page I choose the values like this

Code: Select all

 echo "<a href=\"nalog.php?identworker=$idworker&imeeworker=$nameworker  \">$count</a>";
echo ".)&nbsp;$nameworker&nbsp;$lastnameworker<br>" ;
and in main page i define for example

Code: Select all

$_session['idenworker'] = $identworker;
I cant define session variables in first page because i use result from mysql table that has many rows if i do something like this than session variable will be for the last row

Code: Select all

 
<?    while ($row= mysql_fetch_array($result))
        {
             $idworker = $row["workerID"];
             $nameworker = $row["name"];
         $lastnameworker = $row["lastname"];
         
 
      // this  is wrong                            
          $_session['idworker'] = $idworker;
           $_session['nameworker'] = $nameworker;
 
echo "<a href=\"nalog.php?identworker=$idworker&imeeworker=$nameworker\">$count</a>";
             echo ".)&nbsp;$nameworker&nbsp;$lastnameworker<br>" ;  
                            
             $count++;
         }   ?>
 

Re: I Can`t uderstand how sessios work NEED HELP

Posted: Mon Apr 28, 2008 2:48 pm
by pickle
Ok, I understand that part - thank you.

What is your problem then? I understand you can't assign $_SESSION variables in the loop, but what are you trying to accomplish?

Also, I think you should reference $_SESSION (with capitals) rather than $_session (lowercase).

Re: I Can`t uderstand how sessios work NEED HELP

Posted: Tue Apr 29, 2008 1:56 am
by junelis
I want the values (one mysql row) I choose from the loop to set as session variables. How can i do this ?

Re: I Can`t uderstand how sessios work NEED HELP

Posted: Tue Apr 29, 2008 9:41 am
by pickle
So as you're looping, you want 1 particular row to be stored in $_SESSION?

If so, just make an IF condition inside the loop & if that condition is met, set your $_SESSION variables.

Re: I Can`t uderstand how sessios work NEED HELP

Posted: Tue Apr 29, 2008 12:12 pm
by junelis
Thanks, I found other solution.