Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using Sessions!

Posted on 2004-11-04
5
Medium Priority
?
224 Views
Last Modified: 2006-11-17
Ok, this is my first project in which i use sessions!

My impression from my reading is i just need to use this 'session_start();' i have it placed in the first line on my pages.    This works fine for me on my first page. then when i go to the second page it only half works.. here is my code for the second page..

<?php
session_start();
require 'function.php';

$sql = "select * from vin where vin='".$_SESSION[vin]."'";
$detail_report ='<br>Sql Statement run :' .$sql;
$result = mysql_query($sql,$conn) or die(mysql_error());
$vin = mysql_fetch_array($result);
                  echo '<br> '.$vin['vin'];
                  echo '<br> '.$vin['make'];
                  echo '<br> '.$vin['model'];
                  echo '<br> '.$vin['year'];

$sql = "select * from customer where customer_id='".$vin['currentownerID']."'";
$detail_report ='<br>Sql Statement run :' .$sql;
$result = mysql_query($sql,$conn) or die(mysql_error());
$customer = mysql_fetch_array($result);
                  echo '<br> '.$customer['firstname'].' '.$customer['lastname'];
                  echo '<br> '.$customer['address'];
                  echo '<br> '.$customer['city'].', '.$customer['state'].' '.$customer['zip'];
                  echo '<br> '.$customer['phone'];
                  echo '<br> '.$customer['phone2'];
                  echo '<br> '.$customer['notes'];

$sql = "select * from services";
$result = mysql_query($sql,$conn) or die(mysql_error());
$services = mysql_fetch_array($result);
$servicesrows = mysql_num_rows($result);

echo '<br><br><br>number of rows in the serivce table'.$servicesrows;

while ($catagoryArray = mysql_fetch_array($result)) {
                        // give a name to the fields
                        echo '<br>'. $catagoryArray['name'];

                  }

?>



Ok now the first time this page is run it works if it is refreshed at all or anything the session value is empty so it just pulls empty queries from the databse!? i know the session is still active cause when i go back to the previous page my session still works there.

Is there another command or something that i am missing?
0
Comment
Question by:mightofnight
  • 2
  • 2
5 Comments
 
LVL 2

Expert Comment

by:TheUnkind
ID: 12500807
I think you forgot to put " or ' in $_SESSION[vin]. so it will be $_SESSION['vin'] or $_SESSION["vin"].
0
 
LVL 2

Expert Comment

by:d_tan
ID: 12500880
It is good programming practice to include the index as a string as TheUnkind mentioned.  Read http://jp2.php.net/types.array for more details.

That being said, it is probably the case that php is not quoting the vin for you.

Here's a good tip for ya. . .use print_r.

If you are unsure of what's in the session array. . .

echo "<pre>";
print_r( $_SESSION );
echo "</pre>";
exit();

dtan
0
 
LVL 2

Author Comment

by:mightofnight
ID: 12501083
Hum Thanks guys ireally appreciate the print_r statement whats the <pre></pre> i am not famiular with that tag? Also what does the prupose of the exit(); serve?

The problem must have been with my code cause after i posted i just passed on the variable using get so that i didnt' have to stop.  Now i changed it back to the $_SESSION['vin'] and it works.. it still worked with out the '.  the first examples i learned on never used thoes in array's so i have been tryign to change my habbits :P.


also how do you get the session id? i was going to base a statement on the vin but just realized when i went to write the code that i really needed the session id?
0
 
LVL 2

Accepted Solution

by:
d_tan earned 2000 total points
ID: 12501135
The pre tags are html.  . . it just helps when reading the debug information from the print_r.  exit is the same as die().  It stops the script from executing after that point.  I use those 4 lines for debugging purposes.  It is not necessary to include the pre's or the exit.

there is a function session_id().  Also there is a constant defined as SID.  I suggest reading the php manual on sessions.  This will give you a good handle on sessions and might save ya a few headaches along the way. . .

dtan
0
 
LVL 2

Author Comment

by:mightofnight
ID: 12501151
Thanks d_tan!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month21 days, 2 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question