Jump to content

undefined index


snafudawn

Recommended Posts

Hello all,

 

I'm a newbie to php...2nd semester in and doing an independent study for college.  I have debugged the following error several times but...I just can't figure this one out.  My complete code follows the error.

 

Thank you, in advance.

 

dawn...

 

Notice: Undefined index: id in C:\wamp\www\joke.php on line 24

Could not locate the specified joke ID.

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http:///www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>The Internet Joke Database</title>

<meta http-equiv="content-type"

content="text/html; charset=iso-8859-1" />

</head>

<body>

<?php

 

$dbcnx = @mysql_connect('localhost', 'root', 'twyx1124');

if (!$dbcnx) {

exit('<p>Unable to connect to the ' .

'database server at this time.</p>');

}

 

if (!@mysql_select_db('ijdb')) {

exit('<p>Unable to locate the joke ' .

'database at this time.</p>');

}

 

// Get the joke text from the database

$id = $_GET['id'];

$joke = @mysql_query("SELECT joketext FROM joke WHERE id='$id'");

if (!$joke) {

exit('Unable to load the joke from the database.');

}

 

if (mysql_num_rows($joke) < 1) {

exit('Could not locate the specified joke ID.');

}

 

$joke = mysql_fetch_array($joke);

$joketext = $joke['joketext'];

 

// Filter out HTML code

$joketext = htmlspecialchars($joketext);

 

// If no page specified, default to the first page ($page = 0)

if (!isset($_GET['page'])) {

$page = 0;

} else {

$page = $_GET['page'];

}

 

// Split the text into an array of pages

$textarray = spliti('\[PAGEBREAK]', $joketext);

 

// Select the page we want

$joketext = $textarray[$page];

 

// Bold and italics

$joketext = str_replace(array('', ''), '<strong>', $joketext);

$joketext = str_replace(array('[eb]', '[EB]'), '</strong>', $joketext);

$joketext = str_replace(array('', ''), '<em>', $joketext);

$joketext = str_replace(array('[ei]', '[EI]'), '</em>', $joketext);

 

// Paragraphs and line breaks

$joketext = ereg_replace("\r\n", "\n", $joketext);

$joketext = ereg_replace("\r", "\n", $joketext);

$joketext = ereg_replace("\n\n", '</p><p>', $joketext);

$joketext = ereg_replace("\n", '<br />', $joketext);

 

// Hyperlinks

$joketext = eregi_replace(

'\\[L]([-_./a-z0-9!&%#?+, \'=:;@~]+)\\[EL]', '<a href="\\1">\\1</a>', $joketext);

$joketext = eregi_replace(

'\\[L=([=_./a-z0-9!&%#?+, \'=:;@~]+)]([^\\[]+)\\[EL]', '<a href="\\1">\\2</a>', $joketext);

 

$PHP_SELF = $_SERVER['PHP_SELF'];

 

if ($page != 0) {

$prevpage = $page - 1;

echo "<p><a href=\"$PHP_SELF?id=$id&page=$prevpage\">".

'Previous Page</a></p>';

}

 

echo "<p>$joketext</p>";

 

if ($page < count($textarray) - 1) {

$nextpage = $page + 1;

echo "<p><a href=\"$PHP_SELF?id=$id&page=$nextpage\">".

'Next Page</a></p>';

}

 

?>

<p><a href="indexJokes.php">Back to the front page</a></p>

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/198588-undefined-index/
Share on other sites

This is simply stating that on line 24:

$id = $_GET['id'];

The index of 'id' in the array $_GET doesn't exist.

Which then causes your SQL to fail.

 

Building line 24 so that it would always have an id would be the proper way show the page.

$id = (isset($_GET['id'])) ? (int)$_GET['id'] : 1;

Link to comment
https://forums.phpfreaks.com/topic/198588-undefined-index/#findComment-1042097
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.