I Can`t uderstand how sessios work NEED HELP

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
junelis
Forum Newbie
Posts: 4
Joined: Sat Apr 19, 2008 2:27 pm

I Can`t uderstand how sessios work NEED HELP

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
junelis
Forum Newbie
Posts: 4
Joined: Sat Apr 19, 2008 2:27 pm

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

Post 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++;
         }   ?>
 
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
junelis
Forum Newbie
Posts: 4
Joined: Sat Apr 19, 2008 2:27 pm

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

Post by junelis »

I want the values (one mysql row) I choose from the loop to set as session variables. How can i do this ?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
junelis
Forum Newbie
Posts: 4
Joined: Sat Apr 19, 2008 2:27 pm

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

Post by junelis »

Thanks, I found other solution.
Post Reply