title: Undefined index: id in php
body: <p>I'm having a problem in PHP code. I got some code from an e-book, but when I tried to run the system, it's giving some errors. I fixed some of the errors by giving proper quotation marks, but now I'm stuck with some other errors.</p>
  
  <p>I'd be more than happy if some one helps me with the following error:</p>
  
  <p><strong>Notice: Undefined index: id in C:\xampp\htdocs\auction\index.php on line 4</strong></p>
  
  <p>This is my code for index.php:</p>
  
  <pre><code>&lt;?php
  require("config.php");
  require("functions.php");
  $validid = pf_validate_number($_GET['id'], "value", $config_basedir);
  require("header.php");
  if($validid == 0) {
  $sql = "SELECT items.* FROM items WHERE dateends &gt; NOW()";
  }
  else {
  $sql = "SELECT * FROM items WHERE dateends &gt; NOW()
  AND cat_id = " . $validid . ";";
  }
  $result = mysql_query($sql);
  $numrows = mysql_num_rows($result);
  
  echo "&lt;h1&gt;Items available&lt;/h1&gt;";
  echo "&lt;table cellpadding='5'&gt;";
  echo "&lt;tr&gt;";
  echo "&lt;th&gt;Image&lt;/th&gt;";
  echo "&lt;th&gt;Item&lt;/th&gt;";
  echo "&lt;th&gt;Bids&lt;/th&gt;";
  echo "&lt;th&gt;Price&lt;/th&gt;";
  echo "&lt;/tr&gt;";
  
  if($numrows == 0) {
  echo "&lt;tr&gt;&lt;td colspan=4&gt;No items!&lt;/td&gt;&lt;/tr&gt;";
  }
  
  else {
  while($row = mysql_fetch_assoc($result)) {
  $imagesql = "SELECT * FROM images WHERE
  item_id = " . $row['id'] . " LIMIT 1";
  $imageresult = mysql_query($imagesql);
  $imagenumrows = mysql_num_rows($imageresult);
  echo "&lt;tr&gt;";
  if($imagenumrows == 0) {
  echo "&lt;td&gt;No image&lt;/td&gt;";
  }
  else {
  $imagerow = mysql_fetch_assoc($imageresult);
  echo "&lt;td&gt;&lt;img src='./images/". $imagerow['name'] . "' width='100'&gt;&lt;/td&gt;";
  }
  echo "&lt;td&gt;";
  echo "&lt;a href='itemdetails.php?id="
  . $row['id'] . "'&gt;" . $row['name'] . "&lt;/a&gt;";
  if($_SESSION['USERID'] == $row['user_id']) {
  echo " - [&lt;a href='edititem.php?id="
  . $row['id'] . "'&gt;edit&lt;/a&gt;]";
  }
  echo "&lt;/td&gt;";
  
  $bidsql = "SELECT item_id, MAX(amount) AS
  highestbid, COUNT(id) AS numberofbids FROM bids
  WHERE item_id=" . $row['id'] . " GROUP BY item_id;";
  $bidresult = mysql_query($bidsql);
  $bidrow = mysql_fetch_assoc($bidresult);
  $bidnumrows = mysql_num_rows($bidresult);
  echo "&lt;td&gt;";
  if($bidnumrows == 0) {
  echo "0";
  }
  else {
  echo $bidrow['numberofbids'] . "&lt;/td&gt;";
  }
  echo "&lt;td&gt;" . $config_currency;
  if($bidnumrows == 0) {
  echo sprintf('%.2f', $row['startingprice']);
  }
  else {
  echo sprintf('%.2f', $bidrow['highestbid']);
  }
  
  echo "&lt;/td&gt;";
  echo "&lt;td&gt;" . date("D jS F Y g.iA",
  strtotime($row['dateends'])) . "&lt;/td&gt;";
  echo "&lt;/tr&gt;";
  }
  }
  echo "&lt;/table&gt;";
  require("footer.php");
  ?&gt;
  </code></pre>
  
  <p>Some of you asked, where the id is coming from. I have no idea, but I searched it in other files, and here are they:</p>
  
  <p>bar.php:</p>
  
  <pre><code>&lt;?php
  require("header.php");
  $catsql = "SELECT * FROM categories ORDER BY category ASC;";
  $catresult = mysql_query($catsql);
  echo "&lt;h1&gt;Categories&lt;/h1&gt;";
  echo "&lt;ul&gt;";
  echo "&lt;li&gt;&lt;a href='index.php'&gt;View All&lt;/a&gt;&lt;/li&gt;";
  while($catrow = mysql_fetch_assoc($catresult)) {
  echo "&lt;li&gt;&lt;a href='index.php?id=". $catrow['id'] . "'&gt;" . $catrow['category']. "&lt;/a&gt;                    &lt;/li&gt;";
     }
  echo "&lt;/ul&gt;";
  
  ?&gt;
  </code></pre>
  
  <p>header.php:</p>
  
  <pre><code>&lt;?php
  session_start();
  require("config.php");
  $db = mysql_connect($dbhost, $dbuser, $dbpassword);
  mysql_select_db($dbdatabase, $db);
  ?&gt;
  
  &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
  Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;&lt;?php echo $config_forumsname; ?&gt;&lt;/title&gt;
  &lt;link rel="stylesheet" href="stylesheet.css" type="text/css" /&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="header"&gt;
  &lt;h1&gt;BidTastic Auctions&lt;/h1&gt;
  &lt;div id="menu"&gt;
  &lt;a href="index.php"&gt;Home&lt;/a&gt;
  &lt;?php
  if(isset($_SESSION['USERNAME']) == TRUE) {
  echo "&lt;a href='logout.php'&gt;Logout&lt;/a&gt;";
  }
  else {
  echo "&lt;a href='login.php'&gt;Login&lt;/a&gt;";
  }
  ?&gt;
  &lt;a href="newitem.php"&gt;New Item&lt;/a&gt;
  &lt;/div&gt;
  &lt;div id="container"&gt;
  &lt;div id="bar"&gt;
  &lt;?php require("bar.php"); ?&gt;
  &lt;/div&gt;
  &lt;div id="main"&gt;
  </code></pre>
  
  <p>itemdetails.php</p>
  
  <pre><code>&lt;?php
  session_start();
  include(“config.php”);
  include(“functions.php”);
  $db = mysql_connect($dbhost, $dbuser, $dbpassword);
  mysql_select_db($dbdatabase, $db);
  $validid = pf_validate_number($_GET[‘id’], “redirect”, $config_basedir);
  
  if($_POST[‘submit’]) {
  if(is_numeric($_POST[‘bid’]) == FALSE) {
  header(“Location: “ . $config_basedir
  . “itemdetails.php?id=” . $validid . “&amp;error=letter”);
  }
  
  $theitemsql = “SELECT * FROM items WHERE id = “ . $validid . “;”;
  $theitemresult = mysql_query($theitemsql);
  $theitemrow = mysql_fetch_assoc($theitemresult);
  $checkbidsql = “SELECT item_id, max(amount) AS
  highestbid, count(id) AS number_of_bids FROM
  bids WHERE item_id=” . $validid . “ GROUP BY item_id;”;
  $checkbidresult = mysql_query($checkbidsql);
  $checkbidnumrows = mysql_num_rows($checkbidresult);
  
  if($checkbidnumrows == 0) {
  if($theitemrow[‘startingprice’] &gt; $_POST[‘bid’]) {
  header(“Location: “ . $config_basedir
  . “itemdetails.php?id=” . $validid . “&amp;error=lowprice#bidbox”);
  }
  }
  
  else {
  $checkbidrow = mysql_fetch_assoc($checkbidresult);
  if($checkbidrow[‘highestbid’] &gt; $_POST[‘bid’]) {
  header(“Location: “ . $config_basedir . “itemdetails.php?id=” .
  $validid . “&amp;error=lowprice#bidbox”);
  }
  }
  
  $inssql = “INSERT INTO bids(item_id, amount, user_id) VALUES(“
  . $validid
  . “, “ . $_POST[‘bid’]
  . “, “ . $_SESSION[‘USERID’]
  . “);”;
  mysql_query($inssql);
  
  header(“Location: “ . $config_basedir
  . “itemdetails.php?id=” . $validid);
  }
  else {
  
  
  
  $validid = pf_validate_number($_GET[‘id’], “redirect”,
  $config_basedir);
  require(“header.php”);
  $itemsql = “SELECT UNIX_TIMESTAMP(dateends) AS dateepoch,
  items.* FROM items WHERE id = “ . $validid . “;”;
  $itemresult = mysql_query($itemsql);
  $itemrow = mysql_fetch_assoc($itemresult);
  $nowepoch = mktime();
  $rowepoch = $itemrow[‘dateepoch’];
  if($rowepoch &gt; $nowepoch) {
  $VALIDAUCTION = 1;
  }
  echo “&lt;h2&gt;” . $itemrow[‘name’] . “&lt;/h2&gt;”;
  
  $imagesql = “SELECT * FROM images WHERE item_id = “ . $validid . “;”;
  $imageresult = mysql_query($imagesql);
  $imagenumrows = mysql_num_rows($imageresult);
  
  $bidsql = “SELECT item_id, MAX(amount) AS highestbid,
  COUNT(id) AS number_of_bids FROM bids WHERE item_id=”
  . $validid . “ GROUP BY item_id;”;
  $bidresult = mysql_query($bidsql);
  $bidnumrows = mysql_num_rows($bidresult);
  
  echo “&lt;p&gt;”;
  if($bidnumrows == 0) {
  echo “&lt;strong&gt;This item has had no bids&lt;/strong&gt;
  - &lt;strong&gt;Starting Price&lt;/strong&gt;: “ . $config_currency
  . sprintf(‘%.2f’, $itemrow[‘startingprice’]);
  }
  else {
  $bidrow = mysql_fetch_assoc($bidresult);
  
  echo “&lt;strong&gt;Number Of Bids&lt;/strong&gt;: “
  . $bidrow[‘number_of_bids’] . “
  - &lt;strong&gt;Current Price&lt;/strong&gt;: “ . $config_currency
  . sprintf(‘%.2f’, $bidrow[‘highestbid’]);
  }
  
  echo “ - &lt;strong&gt;Auction ends&lt;/strong&gt;: “
  . date(“D jS F Y g.iA”, $rowepoch);
  
  echo “&lt;/p&gt;”;
  if($imagenumrows == 0) {
  echo “No images.”;
  }
  else {
  while($imagerow = mysql_fetch_assoc($imageresult)) {
  echo “&lt;img src=’./images/” . $imagerow[‘name’] .”’ width=’200’&gt;”;
  }
  }
  
  echo “&lt;p&gt;” . nl2br($itemrow[‘description’]) . “&lt;/p&gt;”;
  
  echo “&lt;a name=’bidbox’&gt;”;
  echo “&lt;h2&gt;Bid for this item&lt;/h2&gt;”;
  
  if(isset($_SESSION[‘USERNAME’]) == FALSE) {
  echo “To bid, you need to log in. Login
  &lt;a href=’login.php?id=” . $validid . “&amp;ref=addbid’&gt;here&lt;/a&gt;.”;
  }
  
  else {
  if($VALIDAUCTION == 1) {
  echo “Enter the bid amount into the box below.”;
  echo “&lt;p&gt;”;
  switch($_GET[‘error’]) {
  case “lowprice”:
  echo “The bid entered is too low.
  Please enter another price.”;
  break;
  case “letter”:
  echo “The value entered is not a number.”;
  break;
  }
  ?&gt;
  
  &lt;form action=”&lt;?php echo pf_script_with_get($SCRIPT_NAME);
  ?&gt;” method=”post”&gt;
  &lt;table&gt;
  &lt;tr&gt;
  &lt;td&gt;&lt;input type=”text” name=”bid”&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input type=”submit” name=”submit” value=”Bid!”&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/form&gt;
  
  &lt;?php
  }
  else {
  echo “This auction has now ended.”;
  }
  
  $historysql = “SELECT bids.amount, users.username FROM bids,
  users WHERE bids.user_id = users.id AND item_id = “
  . $validid . “ ORDER BY amount DESC”;
  $historyresult = mysql_query($historysql);
  $historynumrows = mysql_num_rows($historyresult);
  if($historynumrows &gt;= 1) {
  echo “&lt;h2&gt;Bid History&lt;/h2&gt;”;
  echo “&lt;ul&gt;”;
  while($historyrow = mysql_fetch_assoc($historyresult)) {
  echo “&lt;li&gt;” . $historyrow[‘username’] . “ - “ .
  $config_currency . sprintf(‘%.2f’, $historyrow[‘amount’]) . “&lt;/li&gt;”;
  }
  echo “&lt;/ul&gt;”;
      }
  }
  }
  require(“footer.php”);
  ?&gt;
  </code></pre>
  
  <p>functions.php</p>
  
  <pre><code>&lt;?php
  function pf_script_with_get($script) {
  $page = $script;
  $page = $page . "?";
  foreach($_GET as $key =&gt; $val) {
  $page = $page . $key . "=" . $val . "&amp;";
  }
  
  return substr($page, 0, strlen($page)-1);
  }
  
  function pf_validate_number($value, $function, $redirect) {
  if(isset($value) == TRUE) {
  if(is_numeric($value) == FALSE) {
  $error = 1;
  }
  if($error == 1) {
  header("Location: " . $redirect);
  }
  else {
  $final = $value;
  }
  }
  else {
  if($function == 'redirect') {
  header("Location: " . $redirect);
  }
  if($function == "value") {
  $final = 0;
  }
  }
  return $final;
  }
  ?&gt;
  </code></pre>