Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Undefined index error

Status
Not open for further replies.

NIA2

Technical User
Aug 30, 2006
137
AU
Hi everyone,

I'm trying to build a CMS with PHP and MySQL. I'm at the testing stage but I'm getting the following error when I try to use the supplier form to enter data into my tables (I also get a similar error when trying to enter data into the garment_types form):

Notice: Undefined index: supplier in /Applications/MAMP/htdocs/NewSite/admin/insertNewAttribute.php on line 17


Line 17 refers to the following code:

$supplier = $_POST['supplier'];

I've included the entire code for the page below if it helps troubleshoot. Does anyone know why I'm getting this error?

Appreciate any help.

Code:
<?php
if ($_POST) {
  // include Database classes
  require_once('classes/database.php');
  // escape quotes and apostrophes if magic_quotes_gpc off
  if (!get_magic_quotes_gpc()) {
  foreach($_POST as $key=>$value) {
      $temp = addslashes($value);
      $_POST[$key] = $temp;
      }
    }
  // create Database instance
  $db=new Database('localhost','root','password','catalogue',0);
  // if the "Insert new supplier" button has been clicked
  if (array_key_exists('insSupplier', $_POST)) {
    // code for inserting supplier
	$supplier = $_POST['supplier'];
    $checkName = "SELECT * FROM suppliers
                  WHERE supplier = '$supplier'";
    $result = $db->query($checkName);
    if ($result->num_rows > 0) {
      $supplierAlert = stripslashes($supplier);
      $supplierAlert .= ' is already registered in the suppliers table';
      }
    if (!isset($supplierAlert)) {
      $insertSupplier = "INSERT INTO suppliers
                          SET supplier = '$supplier'";
      $result = $db->query($insertSupplier);
      if ($result) {
        $supplierAlert = stripslashes($supplier);
        $supplierAlert .= ' successfully entered in the suppliers table';
        }
      }
    }
  // if the "Insert new garment type" button has been clicked
  elseif (array_key_exists('insGarType', $_POST)) {
    // code for inserting garment type
	$garment_type = $_POST['garment_type'];
    $checkName = "SELECT * FROM garment_types
                  WHERE garment_type = '$garment_type'";
    $result = $db->query($checkName);
    if ($result->num_rows > 0) {
      $garment_typeAlert = stripslashes($garment_type);
      $garment_typeAlert .= ' is already registered in the garment types table';
      }
    if (!isset($garment_typeAlert)) {
      $insertGarment_type = "INSERT INTO garment_types
                          SET garment_type = '$garment_type'";
      $result = $db->query($insertGarment_type);
      if ($result) {
        $garment_typeAlert = stripslashes($garment_type);
        $garment_typeAlert .= ' successfully entered in the publishers table';
        }
      }
    }
  // close database connection
  $db->close();
  }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"><!--[/URL] InstanceBegin template="/Templates/AdminTemplate.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Insert new garment attribute</title>
<!-- InstanceEndEditable -->
<link href="../main.css" rel="stylesheet" type="text/css" />
<link href="admin.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="../p7pm/p7popmenu.js"></script>
<style type="text/css" media="screen">
<!--
@import url("../p7pm/p7pmh2.css");
-->
</style>
<!--[if lt IE 7]>
<link href="../win_ie.css" rel="stylesheet" type="text/css" />
<![endif]-->
<style type="text/css"></style>
<script language="JavaScript" type="text/JavaScript">
<!--
function Lvl_openWin(u,n,w,h,l,t,c,f) { //v2.2 4LevelWebs
  var x=((screen.width-w)/2);if(c==1){l=x;t=(screen.height-h)/2;}if(c==2){l=x}
	f+=',top='+t+',left='+l;LvlWin=window.open(u,n,f);LvlWin.focus();
}
//-->
</script>
<!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
</head>
<body onload="P7_initPM(1,2,1,-20,10)">
<div id="topstrip"></div>
<div id="navcorp">
  <ul id="p7PMnav">
    <li><a href="#" class="p7PMtrg" id="height">List garments</a>
        <ul>
          <li><a href="#">List suppliers</a></li>
          <li><a href="#">List categories</a></li>
          <li><a href="#">List garment types</a></li>
          <li><a href="#">List colours</a></li>
          <li><a href="#">List sizes</a></li>
        </ul>
    </li>
    <li><a href="#" class="p7PMtrg" id="height">Add new garment</a>
        <ul>
          <li><a href="#">Add new supplier</a></li>
          <li><a href="#">Add new category</a></li>
          <li><a href="#" id="unique">Add new garment type</a></li>
          <li><a href="#">Add new colour</a></li>
          <li><a href="#">Add new size</a></li>
        </ul>
    </li>
    <li><a href="#" class="p7PMtrg" id="height">Register new user</a>
        <ul>
          <li><a href="#">List registered users</a></li>
        </ul>
    </li>
    <li><a href="#" id="height">View Catalogue</a></li>
    <li><a href="#">Logout</a></li>
    <!--[if lte IE 6]><style>#p7PMnav a{height:1em;}#p7PMnav li{height:1em;}#p7PMnav ul li{float:left;clear:both;width:100%}</style><![endif]-->
    <!--[if IE 6]><style>#p7PMnav ul li{clear:none;}</style><![endif]-->
  </ul>
</div>

<!-- InstanceBeginEditable name="EditRegion3" -->
<div id="background">
  <div id="wrapper">
    <div id="innerlayer">
	      <h1>Insert new garment attribute</h1>
    <form id="supplierDets" name="supplierDets" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
        <table class="attrUpdate" border="0" cellpadding="0">

          <tr>
            <th class="attrLabel" scope="row">Insert new supplier </th>
            <td><input name="newSupplier" type="text" class="mediumbox" id="newSupplier" /></td>
            <td><input name="insSupplier" type="submit" id="insSupplier" value="Insert new supplier" /></td>
          </tr>
        </table>
      </form>
	    <?php
  if (isset($supplierAlert))
    echo '<p id="alert">'.$supplierAlert.'</p>';
  ?>

	  <form id="garmentTypeDets" name="garmentTypeDets" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
        <table class="attrUpdate" border="0" cellpadding="0">
          <tr>
            <th class="attrLabel" scope="row">Insert new garment type</th>
            <td><input name="new_garment_type" type="text" class="mediumbox" id="new_garment_type" /></td>
            <td><input name="insGarType" type="submit" id="insGarType" value="Insert new garment type" /></td>
          </tr>
        </table>
      </form>
	    <?php
  if (isset($garment_typeAlert))
    echo '<p id="alert">'.$garment_typeAlert.'</p>';
  ?>

	  <form id="categoryDets" name="categoryDets" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
        <table class="attrUpdate" border="0" cellpadding="0">
          <tr>
            <th class="attrLabel" scope="row">Insert new category </th>
            <td><input name="new_category" type="text" class="mediumbox" id="new_category" /></td>
            <td><input name="insCategory" type="submit" id="insCategory" value="Insert new category" /></td>
          </tr>
        </table>
      </form>
	  <form id="colourDets" name="colourDets" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
        <table class="attrUpdate" border="0" cellpadding="0">
          <tr>
            <th class="attrLabel" scope="row">Insert new colour </th>
            <td><input name="new_colour" type="text" class="mediumbox" id="new_colour" /></td>
            <td><input name="insColour" type="submit" id="insColour" value="Insert new colour" /></td>
          </tr>
        </table>
      </form>
	  <form id="sizeDets" name="sizeDets" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
        <table class="attrUpdate" border="0" cellpadding="0">
          <tr>
            <th class="attrLabel" scope="row">Insert new size </th>
            <td><input name="new_size" type="text" class="mediumbox" id="new_size" /></td>
            <td><input name="insSize" type="submit" id="insSize" value="Insert new size" /></td>
          </tr>
        </table>
      </form>
    </div>
  </div>
</div>
<!-- InstanceEndEditable --><img src="../Images/transparent.gif" width="1" height="1"/>
<div id="footer">&copy; Copyright 2007. All rights reserved. </div>
</body>
<!-- InstanceEnd --></html>
 
This script, by default, outputs a form which submits data back to the script itself.

I don't see a definition for a form field named "supplier" anywhere in the form the script outputs.



Want the best answers? Ask the best questions! TANSTAAFL!
 
The form for entering suppliers was called supplierDets. I changed this form name to be supplier and then tested but still got the same error.

Is this the correction you were referring to when you say:

<<I don't see a definition for a form field named "supplier" anywhere in the form the script outputs.>>
 
I'm not recommending a correction. I'm answering the question
gwh2 said:
Does anyone know why I'm getting this error?

If you get an "undefined index" error when you try to reference an element of $_POST, the most common reason is that a form field by that name was not submitted.

When I look at your code, I see that there is no form field named "supplier" in the HTML output code.

This explains your error.



Want the best answers? Ask the best questions! TANSTAAFL!
 
I think what he meant is that you are not passing a variable called "supplier" when you post the form. Try changing this:

Code:
<td><input name="newSupplier" type="text" class="mediumbox" id="newSupplier" /></td>

to:

Code:
<td><input name="supplier" type="text" class="mediumbox" id="supplier" /></td>
 
Also, you have a logical error in your code.

This line:

[tt]if ($_POST) {[/tt]

will always evaluate to true because $_POST always exists. It may be empty in the case where no POST-method form has been submitted to your script, but it will always exist. So the first part of your script will always be run, even when no form has been submitted. So you're always going to get an "undefined index" error when you run the script without form input.

Change the line to read

[tt]if ($_POST['supplier']) {[/tt]

or some other form-field that will exist only when your form has been submitted.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks, I've taken your advice on board and it's solved the problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top