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

Hi,

In line 2 when you define your session:

TRY:
$_SESSION -> IN UPPERCASE

Instead of:
$_Session -> in lowercase

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());
}

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..

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.

I agree with ddymackek

Did you already pass the get vars for '$_GET' ? If your url has not include 'P_Category', '$_SESSION' 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

$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 = $result;
}
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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.