Jump to content

undefined index - modified


snafudawn

Recommended Posts

    *

          o Personal Message (Online)

 

undefined index

« on: Today at 08:30:24 PM »

 

    * Quote

 

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.

 

Error line 24 is in bold.

 

<!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);

$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/198591-undefined-index-modified/
Share on other sites

Indexes don't populate themselves. You should be aware of how to handle them if they're not set, especially if they are modified by the users themselves.

 

if (isset($_GET['id'])) {
   $id = (int)$_GET['id'];
} else {
   $id = 1; //Default, for example.
}

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.