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 1

Status
Not open for further replies.

dmacster

Technical User
Jan 28, 2005
670
US
I was just happy to have something show up, but I have errors when trying to Add New or even just edit existing. Two forms - first lists all the advertisers (manageSC.php) and the second (formSC.php) is to edit or add.

When I get the manageSC form up, and click on Add New, I get the error
Notice: Undefined index: mode in /hermes/web09/b2871/pow.capegazette/htdocs/formSC.php on line 25
at the very top, and a bunch of errors due to the category names in the body
Notice: Undefined index: category_name in /hermes/web09/b2871/pow.capegazette/htdocs/formSC.php on line 276
>Antique and Auction
and so on until the error at the bottom of the page
Notice: Undefined index: VISIBLE in /hermes/web09/b2871/pow.capegazette/htdocs/formSC.php on line 260
>Visible

Sorry for such a long post, but here's the code for both.
manageSC.php
Code:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once("includes/connection.php");
?>
<table align="center" width="75%" bgcolor="black">
<tr>
<td bgcolor="white" align="center">
<font size="+2"><b>Ad Showcase Management</b></font><br>
</td>
</tr>
</table>
<?php
  if (!empty($_REQUEST['criteria'])) {
    $result = mysql_query(sprintf("SELECT * FROM advertisers WHERE UPPER(sort_name) LIKE '%s%%'", $_REQUEST['criteria']));
  } else {
    $result = mysql_query("SELECT * FROM advertisers ORDER BY sort_name");
  }
  $found = 0;
?>
<table width="75%" align="center" bgcolor="black" cellspacing="1">
<tr>
<td width="75%"><font color="white"><b>Advertiser</b></font></td>
<td align="center"><font color="white"><b>Actions</b></font></td>
</tr>
<tr><td bgcolor="white">&nbsp;</td><td bgcolor="white" align="center"><font size="1"><a href="formSC.php">Add New</a></font></td></tr>
<?php
  while ($rec = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<tr>
<td bgcolor="white"><?php echo $rec["advertiser_name"] ?></td>
<td bgcolor="white" align="center"><font size="1">
<a href="formSC.php?mode=edit&advertiser_id=<?php echo $rec["advertiser_id"] ?>">Edit</a>&nbsp;
<a href="formSC.php?mode=delete&advertiser_id=<?php echo $rec["advertiser_id"] ?>">Delete</a>&nbsp;
</font></td>
</tr>
<?php  
  }
  if (!$found) {
?>
<tr><td colspan="2" bgcolor="white"></td></tr>
<?php
  }
?>
</table>
Output can be viewed here to see the messages. Nothing is live, obviously.

The formSC.php code is
Code:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once("includes/connection.php");
$TYPE_COLUMN = 0;
$LABEL_COLUMN = 1;
$FIELD_COLUMN = 2;
$VALUE_COLUMN = 3;
$ADVERTISERS_OBJECTS = array("category_name" => array("checkbox", "Category", "category_name", "Antique and Auction,Automobile,Construction and Services,Entertainment / Movie / Video,Finance,
Food and Drink,Health / Fitness / Beauty,Home Products / Services,Lawn and Garden,Local Events,Medical,Real Estate,Religion,Restaurants,Retail"),
                        "name" => array("textfield", "Advertiser Name", "advertiser_name", ""),
                        "sortname" => array("textfield", "Sort Name", "sort_name", ""),
                        "imag1" => array("textfield", "Image 1 (URL)", "img1", ""),
                        "img1width" => array("textfield", "Image 1 Width", "img1width", ""),
                        "img2height" => array("textfield", "Image 1 Height", "img1height", ""),
                        "img2" => array("textarea", "Image 2 (URL)", "img2", ""),
                        "img2width" => array("textfield", "Image 2 Width", "img2width", ""),
                        "img2height" => array("textfield", "Image 2 Height", "img2height", ""),
                        "visible" => array("radio", "Visible", "VISIBLE", "Visible, Not Visible")
                        );


$METHOD_TABLE = array("NAME" => array("onBlur" => "fillsortname()"));
$rec = array();
$mode = $_REQUEST["mode"];
switch ($mode) {
  case "delete":
  case "edit":
    $r = mysql_query(sprintf("SELECT * FROM advertisers WHERE advertiser_id=%s", $_REQUEST["advertiser_id"]));
    $rec = mysql_fetch_array($r, MYSQL_ASSOC);
    break;

  case "insert":
//    printf("<div align=\"center\"><a href=\"manageSC.php\">Showcase Management</a></div><p>");
    printf("%s<p>\n", write_insert($_POST));
//    $result = mysql_query(write_insert($_POST));
//    $_POST["advertiser_id"] = mysql_insert_id($result);
    $result = mysql_query(sprintf("%s; select advertiser_id=@@IDENTITY", write_insert($_POST)));
	$tmprec = mysql_fetch_array($result, MYSQL_ASSOC);
	$_POST["advertiser_id"] = $tmprec["advertiser_id"];
    break;

  case "update":
//    printf("<div align=\"center\"><a href=\"manageSC.php\">Showcase Management</a></div><p>");
    printf("%s<p>\n", write_update($_POST));
    $result = mysql_query(write_update($_POST));
    break;
    
  case "confirm":
//    printf("<div align=\"center\"><a href=\"manageSC.php\">Showcase Management</a></div><p>");
    $result = mysql_query(sprintf("DELETE FROM advertisers WHERE advertiser_id = %d", $_POST["advertiser_id"]));
    break; 
    
  default:
    $mode = "add";
}


?>
<html>
<head>
<?php
write_java_functions();
?>
</head>
<body>
<?php
printf("<div align=\"center\"><a href=\"manageSC.php\">Showcase Management</a></div><p>");

if (!in_array($mode, array("insert", "update", "confirm"))) {
  switch ($mode) {
     case "delete":
       $mode = "confirm";
       break;
     case "edit":
       $mode = "update";
       break;
     case "add":
       $mode = "insert";
       break;
  }
  draw_advertiser_form($rec, $mode);
} else {
  if (is_bool($result) && !$result) {
?>
There was an error processing your request:<p>
<?php echo mysql_get_last_message() ?><p>
<?php
  } else {
?>
Record updated!
<?php
  }
}
?>
</body>
</html>
<?php
function write_java_functions() {
  global $ADVERTISERS_OBJECTS, $TYPE_COLUMN, $LABEL_COLUMN, $FIELD_COLUMN, $VALUE_COLUMN;
?>
<script language="javascript">
<?php
  foreach ($ADVERTISERS_OBJECTS as $key => $data) {
    switch($data[$TYPE_COLUMN]) {
      case "checkbox":
        printf("function update%s() {\n", $data[$FIELD_COLUMN]);      
        print("  form = window.document.advertiser_form;\n");
        $items = explode(",", $data[$VALUE_COLUMN]);
        if (count($items)) {
          printf("  form.%s.value = 0;\n", $data[$FIELD_COLUMN]);
          $val = 0;
          foreach ($items as $item) {
            printf("  if (form.%s_%d.checked) {\n", $data[$FIELD_COLUMN], $val);
            printf("    form.%s.value = eval(form.%s.value) + eval(form.%s_%d.value);\n", $data[$FIELD_COLUMN], $data[$FIELD_COLUMN], $data[$FIELD_COLUMN], $val);
            print("  }\n\n");
            $val++;
          }
        }
        print("}\n\n");
        break;
    }
  } 
  print("function verifyData() {\n");
  print("  form = window.document.advertiser_form;\n");
  foreach ($ADVERTISERS_OBJECTS as $key => $data) {
    switch($data[$TYPE_COLUMN]) {
      case "checkbox":
        printf("  update%s();\n", $data[$FIELD_COLUMN]);      
    }
  }
  print("  fillsortname();\n");
  print("  form.submit();\n");
  print("}\n\n");
  
?>


function fillsortname() {
  form = window.document.advertiser_form;
  if (form.SORTNAME.value == "") {
    form.SORTNAME.value = form.NAME.value.toUpperCase();
  }
}

</script>
<?php  
}


function write_insert($post) {
  global $ADVERTISERS_OBJECTS, $TYPE_COLUMN, $LABEL_COLUMN, $FIELD_COLUMN, $VALUE_COLUMN;
  
  $sql = "INSERT INTO advertisers (%s) VALUES (%s)";
  $fields = "";
  $values = "";
  foreach ($ADVERTISERS_OBJECTS as $key => $data) {
    if (in_array($data[$FIELD_COLUMN], array_keys($post))) { 
      $fields .= $data[$FIELD_COLUMN] . ",";
      if (!is_numeric($post[$data[$FIELD_COLUMN]])) {
        $values .= sprintf("'%s',", str_replace("'", "''", stripslashes($post[$data[$FIELD_COLUMN]])));
      } else {
        $values .= $post[$data[$FIELD_COLUMN]] . ",";
      }
    }    
  }
  if ($len = strlen($fields)) {
    $fields = substr($fields, 0, $len - 1);
  }
  if ($len = strlen($values)) {
    $values = substr($values, 0, $len - 1);
  }
  
  return sprintf($sql, $fields, $values);
}

function write_update($post) {
  global $ADVERTISERS_OBJECTS, $TYPE_COLUMN, $LABEL_COLUMN, $FIELD_COLUMN, $VALUE_COLUMN;

  $sql = "UPDATE advertisers SET %s WHERE advertiser_id=%d";
  $fields = "";

  foreach ($ADVERTISERS_OBJECTS as $key => $data) {
    if (in_array($data[$FIELD_COLUMN], array_keys($post))) { 
      $fields .= $data[$FIELD_COLUMN] . " = ";
      if (!is_numeric($post[$data[$FIELD_COLUMN]])) {
//        $fields .= sprintf("\'%s\',", str_replace("'", "''", $post[$data[$FIELD_COLUMN]]));
        $fields .= sprintf("'%s',", str_replace("'", "''", stripslashes($post[$data[$FIELD_COLUMN]])));
      } else {
        $fields .= $post[$data[$FIELD_COLUMN]] . ",";
      }
    }    
  }
  if ($len = strlen($fields)) {
    $fields = substr($fields, 0, $len - 1);
  }
  return sprintf($sql, $fields, $post["advertiser_id"]);

}

function write_delete($post) {
  global $ADVERTISERS_OBJECTS, $TYPE_COLUMN, $LABEL_COLUMN, $FIELD_COLUMN, $VALUE_COLUMN;
  
  $sql = "DELETE advertisers WHERE advertiser_id=%d";
  return sprintf($sql, $post["advertiser_id"]);
}


function draw_advertiser_form($rec, $mode) {
  global $ADVERTISERS_OBJECTS, $TYPE_COLUMN, $LABEL_COLUMN, $FIELD_COLUMN, $VALUE_COLUMN;
?>
<form name="advertiser_form" method="post">
<input type="hidden" name="mode" value="<?php echo $mode ?>">
<table width="75%" style="border: 2px solid black" align="center">
<tr style="background-color: black; color: white; font-weight:bold; font-size:14pt">
<td colspan="2" align="center">
ADVERTISER INFORMATION
</td>
</tr>
<?php  
  foreach ($ADVERTISERS_OBJECTS as $key => $data) {
?>
<tr style="border: 2px solid black">
<td align="right" valign="top"><?php echo $data[$LABEL_COLUMN] ?></td>
<td>
<?php
    draw_object($data, $rec);
?>
</td>
</tr>

<?php    
  }
?>
<tr style="border: 2px solid black">
<td colspan="2" align="center">
<input type="submit" value="<?php echo $mode == "confirm" ? "Confirm Delete" : "Save" ?>" onClick="verifyData()">
</td>
</tr>
</table>
<?php
  if ($rec["advertiser_id"] > 0) {
?>
<input type="hidden" name="advertiser_id" value="<?php echo $rec["advertiser_id"] ?>">
<?php
  }
?>
</form>
<?php
}

function draw_object($data, $rec) {
  global $ADVERTISERS_OBJECTS, $TYPE_COLUMN, $LABEL_COLUMN, $FIELD_COLUMN, $VALUE_COLUMN;
    switch($data[$TYPE_COLUMN]) {
      case "radio":
        $items = explode(",", $data[$VALUE_COLUMN]);
        $val = 1;
        foreach ($items as $item) {
?>                     
<input type="<?php echo $data[$TYPE_COLUMN] ?>" name="<?php echo $data[$FIELD_COLUMN] ?>" value="<?php echo $val ?>" <?php echo write_field_methods($data[$FIELD_COLUMN]) ?><?php echo ($rec[$data[$FIELD_COLUMN]] == $val) ? " checked" : "" ?>><?php echo $item ?><br>
<?php
          $val++;
        }
        break;

      case "checkbox":
        $items = explode(",", $data[$VALUE_COLUMN]);
?>
<input type="hidden" name="<?php echo $data[$FIELD_COLUMN] ?>" value="0">
<?php      
        $val = 0;
        foreach ($items as $item) {
          $fieldname = $data[$FIELD_COLUMN] . "_" . $val;
          $bit = pow(2,$val++);
?>                     
<input type="<?php echo $data[$TYPE_COLUMN] ?>" name="<?php echo $fieldname ?>" value="<?php echo $bit ?>" onClick="update<?php echo $data[$FIELD_COLUMN] ?>()" <?php echo write_field_methods($data[$FIELD_COLUMN]) ?> <?php echo (($rec[$data[$FIELD_COLUMN]] & $bit) == $bit) ? " checked" : "" ?>><?php echo $item ?><br>
<?php
        }
        break;

      case "textarea":
?>                     
<textarea name="<?php echo $data[$FIELD_COLUMN] ?>" rows="5" cols="60" <?php echo write_field_methods($data[$FIELD_COLUMN]) ?>><?php echo $rec[$data[$FIELD_COLUMN]] ?></textarea><p>
<?php
        break;

      case "textfield":
?>                     
<input type="<?php echo $data[$TYPE_COLUMN] ?>" name="<?php echo $data[$FIELD_COLUMN] ?>" size="60" value="<?php echo $rec[$data[$FIELD_COLUMN]] ?>" <?php echo write_field_methods($data[$FIELD_COLUMN]) ?>><br>
<?php
        break;
        
      default:
?>                     
<input type="<?php echo $data[$TYPE_COLUMN] ?>" name="<?php echo $data[$FIELD_COLUMN] ?>" value="<?php echo $rec[$data[$FIELD_COLUMN]] ?>" <?php write_field_methods($data[$FIELD_COLUMN]) ?>><br>
<?php
    }
}

function write_field_methods($field) {
  global $METHOD_TABLE;
  
  $ret = "";
  if (in_array($field, array_keys($METHOD_TABLE)) && ($methods = $METHOD_TABLE[$field])) {
    foreach ($methods as $evt => $code) {
      $ret .= sprintf("%s=\"%s\" ", $evt, $code);
    }
  }
  return $ret;
}
?>

I'm not sure where I've lost it, but please point me in the right direction.

Thanks,
Donna
 
The "index" the error is referring to is the key for the array.

Code:
Line 25: $mode = $_REQUEST["mode"];
Code:
Line 276: <input type="<?php echo $data[$TYPE_COLUMN] ?>" name="<?php echo $fieldname ?>" value="<?php echo $bit ?>" onClick="update<?php echo $data[$FIELD_COLUMN] ?>()" <?php echo write_field_methods($data[$FIELD_COLUMN]) ?> <?php echo (($rec[$data[$FIELD_COLUMN]] & $bit) == $bit) ? " checked" : "" ?>><?php echo $item ?><br>
Code:
Line 260: <input type="<?php echo $data[$TYPE_COLUMN] ?>" name="<?php echo $data[$FIELD_COLUMN] ?>" value="<?php echo $val ?>" <?php echo write_field_methods($data[$FIELD_COLUMN]) ?><?php echo ($rec[$data[$FIELD_COLUMN]] == $val) ? " checked" : "" ?>><?php echo $item ?><br>

This is because the vars are empty somehow. i didn't scruitinze your code to tell you when and where to define these vars, but you can just turn the notices off, as they will always show when you haven't defined a variable. It's good practice to always initialize your variables, but in PHP it's less and less necessary.

To turn off the notices, use
Code:
//will show everything except notices
error_reporting(E_ALL ^ E_NOTICE);
and take a look at
 
it is normal to suppress notices for production code. but nevertheless best practice is to get rid of them before moving code into production.
 
Gotcha - I turned them off. I must have made an error with the SELECT and the array - the category name is in another table, so when I try to add new I get an error
Code:
INSERT INTO advertisers (category_name,advertiser_name,sort_name,img1,img1width,img2height,img2,img2width,VISIBLE) VALUES (16,'Test this','testMeNow','images/7616.jpg',300,200,'','',2)


Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /hermes/web09/b2871/pow.capegazette/htdocs/formSC.php on line 39

Same message when I try to edit - I suppose that's why when I go to edit, the category name is NOT checked, but the visible field IS part of advertisers table and neither visible or not visible is checked.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top