Page 1 of 1

Trouble using the Session variables

Posted: Tue Jan 15, 2008 1:16 pm
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

Re: Trouble using the Session variables

Posted: Tue Jan 15, 2008 1:22 pm
by RobertGonzalez
Post some code. Trying to help troubleshoot code problems is a little difficult without the code behind it. ;)

Re: Trouble using the Session variables

Posted: Tue Jan 15, 2008 1:27 pm
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++;
}
 
 
?>

Re: Trouble using the Session variables

Posted: Tue Jan 15, 2008 1:34 pm
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.

Re: Trouble using the Session variables

Posted: Tue Jan 15, 2008 1:43 pm
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++;
} 
?>

Re: Trouble using the Session variables

Posted: Tue Jan 15, 2008 1:45 pm
by RobertGonzalez

Re: Trouble using the Session variables

Posted: Tue Jan 15, 2008 1:56 pm
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

Re: Trouble using the Session variables

Posted: Tue Jan 15, 2008 2:06 pm
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.

Re: Trouble using the Session variables

Posted: Tue Jan 15, 2008 2:25 pm
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.

Re: Trouble using the Session variables

Posted: Tue Jan 15, 2008 3:29 pm
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..

Re: Trouble using the Session variables

Posted: Tue Jan 15, 2008 3:39 pm
by RobertGonzalez
Try it. If it doesn't work, tell us and post the code you are using so we can help you.