954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Session Help

hi i have one page where i will get Categories of Products and i want to make session of those Categories..But when i referesh page session break...my code is..

session_start();
	$_Session['P_Category'] = $_GET['P_Category']; ////line 8
	$P_Category =$_Session['P_Category'];
	
	$query = "select * from tblProducts_info where P_Category = '$P_Category'";

$result = mysql_query($query);

if(!$result)
{
	die('query:invalid Query: '.mysql_error());
}


and error comes that...
Notice: Undefined index: P_Category in C:\wamp\www\New\electronix\Breaks\Center_Content.php on line 8

Farhad.idrees
Junior Poster in Training
63 posts since Dec 2010
Reputation Points: 11
Solved Threads: 0
 

Hi,

In line 2 when you define your session:

TRY:
$_SESSION['P_Category'] -> IN UPPERCASE

Instead of:
$_Session['P_Category'] -> in lowercase

klemme
Posting Whiz in Training
265 posts since Mar 2011
Reputation Points: 18
Solved Threads: 7
 

The code should look like this after editing

session_start();
	$_SESSION['P_Category'] = $_GET['P_Category']; ////line 8
	$P_Category = $_SESSION['P_Category'];
 
	$query = "select * from tblProducts_info where P_Category = '$P_Category'";
 
$result = mysql_query($query);
 
if(!$result)
{
	die('query:invalid Query: '.mysql_error());
}
Nahiyan
Junior Poster in Training
65 posts since Sep 2009
Reputation Points: 17
Solved Threads: 5
 

I did it....but i still get this error
Notice: Undefined index: P_Category in C:\wamp\www\New\electronix\Breaks\Center_Content.php on line 8

i want to store P_Category in session.what should i do?..when i referesh page so error comes..

Farhad.idrees
Junior Poster in Training
63 posts since Dec 2010
Reputation Points: 11
Solved Threads: 0
 

session_start() must be the first thing on your page.

<?php
session_start();  // nothing can be before this line
// which appears to be line 7 in your program.  you should post full codes for debugging info, because your line 8 is our line 2.  that should do it for you though, move your session_start() call to be line 1 of your program.
ddymacek
Posting Whiz
317 posts since Jun 2010
Reputation Points: 36
Solved Threads: 64
 

I agree with ddymackek

Nahiyan
Junior Poster in Training
65 posts since Sep 2009
Reputation Points: 17
Solved Threads: 5
 

Did you already pass the get vars for '$_GET['P_Category']' ? If your url has not include 'P_Category', '$_SESSION['P_Category']' will undefined variable.

$_SESSION['P_Category'] = isset($_GET['P_Category']) ? $_GET['P_Category'] : 'default'; // if you've already P_Category with url, assign to the session, otherwise, default value will assign to the session
Zero13
Practically a Master Poster
624 posts since Jan 2009
Reputation Points: 120
Solved Threads: 139
 

$query = "select * from tblProducts_info where P_Category = '{$P_Category}'";
this will solve the appeared error

if you want it to be saved in the session after the sql fetched like:
while($result = mysql_fetch_array(mysql_query($query)))
{
$_SESSION['p_category'] = $result['P_Category'];
}
this is a simple way and you can make more comlicated and advanced using OOP by making a class database doing this for you... aw the first line i wrote is what will help you avoid the error

mamdouh ramadan
Newbie Poster
11 posts since Aug 2011
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: