SESSION error...showing undefined index pls help

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
abrahamrkj
Forum Newbie
Posts: 14
Joined: Mon Nov 14, 2011 10:59 pm

SESSION error...showing undefined index pls help

Post by abrahamrkj »

My first php is....searchbook.php
(getting variable 'k' frm txtbox)
<?php

$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM bookup WHERE";
$i = 0;
foreach ($terms as $each)
{
$i++;

if($i == 1)
$query .= " keywords LIKE '%$each%'";
else
$query .= " OR keywords LIKE '%$each%'";
}
mysql_connect("localhost","root","12345") or die("db prob");
mysql_select_db("games") or die("db prob test");
$query = mysql_query($query);
$numrows = mysql_num_rows($query);

if($numrows > 0)
{
while ($row = mysql_fetch_assoc($query))
{

$title = $row['gamename'];
$description = $row['gamedes'];
$keywords = $row['keywords'];

}
echo $title,$description;
$_SESSION['title'] = $title;

echo '<form method="GET" action="msearchbookh.php"><input type="submit" value="Have It!"></form><form method="GET" action="msearchbookw.php"><input type="submit" value="Want It!"></form>';


}
else
{
echo "No result FOund ";
}
?>


My second php is.... msearchbookh.php
<?php




$test = $_SESSION['title'];




$con = mysql_connect("localhost","root","abraham");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("test", $con);

mysql_query("UPDATE users SET haves = '$test'
WHERE name = '$sesname'");
echo "$test added to haves of $sesname";
?>
in this i hav undefined index in nxt php....pls help


Thanks in advance
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: SESSION error...showing undefined index pls help

Post by social_experiment »

If you can wrap your php code in tags using the php code button i'll help you :) Sorry to be a jerk about this but it makes your code easier to read.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
abrahamrkj
Forum Newbie
Posts: 14
Joined: Mon Nov 14, 2011 10:59 pm

Re: SESSION error...showing undefined index pls help

Post by abrahamrkj »

social_experiment wrote:If you can wrap your php code in tags using the php code button i'll help you :) Sorry to be a jerk about this but it makes your code easier to read.
..
My msearchbook.php......
<?php
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM bookup WHERE";
$i = 0;
foreach ($terms as $each)
{
$i++;
if($i == 1)
$query .= " keywords LIKE '%$each%'";
else
$query .= " OR keywords LIKE '%$each%'";
}
mysql_connect("localhost","root","12345") or die("db prob");
mysql_select_db("games") or die("db prob test");
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if($numrows > 0)
{
while ($row = mysql_fetch_assoc($query))
{
$title = $row['gamename'];
$description = $row['gamedes'];
$keywords = $row['keywords'];
}
echo $title,$description;
$_SESSION['title'] = $title;
echo '<form method="GET" action="msearchbookh.php"><input type="submit" value="Have It!"></form><form method="GET" action="msearchbookw.php"><input type="submit" value="Want It!"></form>';
}
else
{
echo "No result FOund ";
}
?>
My second one is msearchbookh.php(i get frm above form)
<?php
$test = $_SESSION['title']; //ERROR HERE "Undefined index,.,,,,"
$con = mysql_connect("localhost","root","abraham");
$test = $_SESSION['title'];
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("test", $con);
$sql = "UPDATE users SET haves = '" . mysql_real_escape_string($test) . "' where
name = '" . mysql_real_escape_string($sesname) . "' ";
$qry = mysql_query($sql);

if ($qry) {
echo 'added';
}
?>
Thankss..
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: SESSION error...showing undefined index pls help

Post by social_experiment »

You have to use session_start() at the beginning of each page if you want to use sessions.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
abrahamrkj
Forum Newbie
Posts: 14
Joined: Mon Nov 14, 2011 10:59 pm

Re: SESSION error...showing undefined index pls help

Post by abrahamrkj »

social_experiment wrote:You have to use session_start() at the beginning of each page if you want to use sessions.

after i added session_start();
i got this error
Notice: Undefined index: title in C:\wamp\www\html\new\msearchbookh.php in that "$test = $_SESSION['title']; "....hlp pls
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: SESSION error...showing undefined index pls help

Post by social_experiment »

Try this bit of code in your script

Code: Select all

<php
 (isset($_SESSION['title'])) ? $title = $_SESSION['title'] : $title = 'Something else';
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
abrahamrkj
Forum Newbie
Posts: 14
Joined: Mon Nov 14, 2011 10:59 pm

Re: SESSION error...showing undefined index pls help

Post by abrahamrkj »

social_experiment wrote:Try this bit of code in your script

Code: Select all

<php
 (isset($_SESSION['title'])) ? $title = $_SESSION['title'] : $title = 'Something else';
?>


Thank u so much this saved me! :) :)
Post Reply