Trouble using the Session variables

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
gzero36
Forum Newbie
Posts: 6
Joined: Tue Jan 15, 2008 1:07 pm

Trouble using the Session variables

Post by gzero36 »

Ok first off I am very new to php so sorry if this is a dump question. Right now I have a php file that will connect to a mysql database and depending on the previous search post it will display all data that is similar to the search. So for example my table has name and some other stuff in it. When searching for a name I may get more then one person. I know I can use an Id but it is easier for someone to just type in a first name or what have you. Now this particular file will also have an update button so below each set of information there will be an option to update. The problem is that When I click update it will only display the last set of information. I have tried using Sessions and I currently have all information about the User in an object class.

So in short I need to know how to specify with the update button which set of information I actually want to update.

Thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Trouble using the Session variables

Post by RobertGonzalez »

Post some code. Trying to help troubleshoot code problems is a little difficult without the code behind it. ;)
gzero36
Forum Newbie
Posts: 6
Joined: Tue Jan 15, 2008 1:07 pm

Re: Trouble using the Session variables

Post by gzero36 »

Code: Select all

<?php
session_start();
require("include/connect.php");
include ("employee.class.inc");
 
 
$field = addslashes($_POST["field"]);
$find = addslashes($_POST["find"]);
 
//echo $find;
// if value is empty then display all contents
if($find == ""){
$query="SELECT * FROM employee_data";
$result=mysql_query($query);}
 
else {
$query="SELECT * FROM employee_data WHERE $field LIKE '%$find%'";
$result=mysql_query($query);}
 
$num=mysql_numrows($result);
mysql_close();
$emp_array[] = $num;
$i=0;
while ($i < $num) {
 
$first = mysql_result($result,$i,"f_name");
$last = mysql_result($result,$i,"l_name");
$title = mysql_result($result,$i,"title");
$email = mysql_result($result,$i,"email");
$cell_phone = mysql_result($result,$i,"cell_phone");
$office_phone = mysql_result($result,$i,"office_phone");
$department = mysql_result($result,$i,"department");
$emp_id = mysql_result($result,$i,"emp_id");
 
$$emp_id = new Employee;
 
$$emp_id->setFirst($first);
$$emp_id->setLast($last);
$$emp_id->setEmp_id($emp_id);
$$emp_id->setTitle($title);
$$emp_id->setEmail($email);
$$emp_id->setCell_phone($cell_phone);
$$emp_id->setOffice_phone($office_phone);
 
echo "<b>$first $last</b><br> $department<br>$title<br>Email: $email<br>Office Phone: $office_phone<br>Cell Phone: $cell_phone<br><br>";
?>
 
<html>
<body>
<form name="fa" action="amend2.php" method="POST">
    <input type="submit" name="Amend" value="Amend" />
    </form> 
</body>
</html>
 
<?php
 
$emp_array[$i] = "$emp_id";
$_SESSION["$emp_id"] = serialize($$emp_id);
$i++;
}
 
 
?>
gzero36
Forum Newbie
Posts: 6
Joined: Tue Jan 15, 2008 1:07 pm

Re: Trouble using the Session variables

Post by gzero36 »

Sorry for it being sloppy. Also I had session variables for them all but then tried to make it work with an object class which is where I am at now.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Trouble using the Session variables

Post by RobertGonzalez »

Your code looks a little jacked up. Try something like this and see if it helps.

Code: Select all

<?php
session_start();
require 'include/connect.php';
include 'employee.class.inc';
 
// You really should check to make sure there are POST array keys with these names
$field = addslashes($_POST["field"]);
$find = addslashes($_POST["find"]);
 
//echo $find;
// if value is empty then display all contents
if ($find == "") {
  $query="SELECT * FROM employee_data";
  $result=mysql_query($query);
} else {
  $query="SELECT * FROM employee_data WHERE $field LIKE '%$find%'";
  $result=mysql_query($query);
}
 
$num=mysql_numrows($result);
mysql_close();
$emp_array[] = $num;
$i=0;
while ($i < $num) {
  $first = mysql_result($result,$i,"f_name");
  $last = mysql_result($result,$i,"l_name");
  $title = mysql_result($result,$i,"title");
  $email = mysql_result($result,$i,"email");
  $cell_phone = mysql_result($result,$i,"cell_phone");
  $office_phone = mysql_result($result,$i,"office_phone");
  $department = mysql_result($result,$i,"department");
  $emp_id = mysql_result($result,$i,"emp_id");
  
  $employee = new Employee;
  $employee->setFirst($first);
  $employee->setLast($last);
  $employee->setEmp_id($emp_id);
  $employee->setTitle($title);
  $employee->setEmail($email);
  $employee->setCell_phone($cell_phone);
  $employee->setOffice_phone($office_phone);
  echo "<b>$first $last</b><br> $department<br>$title<br>Email: $email<br>Office Phone: $office_phone<br>Cell Phone: $cell_phone<br><br>";
?>
 
<html>
<body>
<form name="fa" action="amend2.php" method="POST">
    <input type="submit" name="Amend" value="Amend" />
    </form>
</body>
</html>
 
<?php
  $emp_array[$i] = $emp_id;
  $_SESSION[$emp_id] = serialize($employee);
  $i++;
} 
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Trouble using the Session variables

Post by RobertGonzalez »

gzero36
Forum Newbie
Posts: 6
Joined: Tue Jan 15, 2008 1:07 pm

Re: Trouble using the Session variables

Post by gzero36 »

Thanks for the link on Serialization I will read up on that.. The code that you resubmitted has: $employee = new Employee;

If there were more then one employ that was found in the search would they not be overwriting themselves instead of creating new objects for each employee?

thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Trouble using the Session variables

Post by RobertGonzalez »

Yes, on each iteration. But the way you were doing it before was assigning a temp var (probably a numeric, which is not allowed in PHP) based on the user id. THis was creating a crapload of objects all with variable names that were doing to exact same thing as the code I posted, for the most part.
gzero36
Forum Newbie
Posts: 6
Joined: Tue Jan 15, 2008 1:07 pm

Re: Trouble using the Session variables

Post by gzero36 »

So what would I need to do for my code to do this...

For example I do a search three records are found. And is displayed as

Name
Title
Position
Phone

Amend // submit button

I now would have three sets of these I want to be able to click the Amend button for any set and populate the appropriate data on another page.
gzero36
Forum Newbie
Posts: 6
Joined: Tue Jan 15, 2008 1:07 pm

Re: Trouble using the Session variables

Post by gzero36 »

Ok what I think I will do is pass the emp_id through the url and from this repopulate the rest of the variables. That seems like it will work fine..
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Trouble using the Session variables

Post by RobertGonzalez »

Try it. If it doesn't work, tell us and post the code you are using so we can help you.
Post Reply