Jump to content

[SOLVED] Interesting 'Undefined index:' error


simcoweb

Recommended Posts

I'm creating a dynamic form where the fields are all populated by queries to the mysql database. Here's the error message received when doing a search without selecting any property types:

 

Notice: Undefined index: propertytypes in /home2/xxxxxxx/public_html/brokers/search-propertytypes.php on line 15

 

which refers to this section of the form processing code (just showing the important parts):

 

if (isset($_POST['search'])) {
// query for extracting lenders from amember database
$category = strip_tags(trim($_POST['category']));
$loantype = strip_tags(trim($_POST['loan_type']));
$propertytype = strip_tags(trim($_POST['propertytypes'])); [b][color=red]<-- This is line 15[/color][/b]

$sql = "SELECT * FROM amember_members WHERE is_lender='Yes' AND '$category' IN (category_1, category_2, category_3, category_4, category_5) AND '$loantype' IN (loan_type, loan_type2, loan_type3, loan_type4, loan_type5) AND '$propertytype' IN (propertytype, property_type2, property_type3, property_type4, property_type5, property_type6, property_type7, property_type8, property_type9, property_type10, property_type11, property_type12, property_type13, property_type14, property_type15, property_type16, property_type17, property_type18, property_type19, property_type20, property_type21, property_type22, property_type23, property_type24, property_type25, property_type27, property_type28) ORDER BY top_3 LIMIT 10";

$results = mysql_query($sql)or die(mysql_error());
$numrows = mysql_num_rows($results);

 

 

The form function itself actually works perfectly. Here's the code for it:

 

function search_form3()  {
  echo "<form method='POST' action='search-propertytypes.php' onsubmit='return validateIt(this)'>
  <select id='cat' name='category' onchange=\"opt(this.value,'cat','Select A Valid Category!')\">\r";
  echo "<option selected value='ignore'>Select Category</option>";
  $sql = "SELECT category FROM categories";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      while($row = mysql_fetch_assoc($result)) {
        echo "<option value='{$row['category']}' name='{$row['category']}'>{$row['category']}</option>\n\r";
      }
    }
  }
  echo "</select>\n\r";
  
  // loan type form
  echo "<select id=\"cat2\" name='loan_type' onchange=\"opt(this.value,'cat2','Select A Valid Loan Type!')\">\r";
  echo "<option selected value='ignore'>Select Loan Type</option>";
  $sql2 = "SELECT loantype FROM loan_type";
  if ($results2 = mysql_query($sql2)) {
if (mysql_num_rows($results2)) {
	while($row2 = mysql_fetch_assoc($results2)) {
		echo "<option value='{$row2['loantype']}' name='{$row2['loantype']}'>{$row2['loantype']} </option>\n\r";
	}
}
}
  echo "</select>";
  
// property type form
$sql3 = "SELECT propertytypes FROM property_types";
if($results3 = mysql_query($sql3)) {
if(mysql_num_rows($results3)) {
	echo "<p class='bodytext'><b>Select property type(s):</b><br/>\n";
echo "<table width='300' border='0'>";
	while($row3 = mysql_fetch_assoc($results3)) {
		echo "<tr><td class='bodytext'>{$row3['propertytypes']}</td><td> <input type='checkbox' name='propertytypes' value='{$row3['propertytypes']}'></td></tr>\n";

		}

	}

}
	echo "</table>";
	echo "<input type='submit' name='search' value='Search'>\n</form>";
}

 

Now, IF someone doesn't choose any of the property types then the error occurs. IF they DO select any or multiple selections of the property types checkboxes then no error occurs.

 

This may be a result of the fact that 'propertytypes' should be held as an array since they can which may require that I place the array indicators [] in each checkbox for 'propertytypes'. Or, perhaps an 'if' statement that excludes it from the search if no boxes are checked. The checkboxes are not 'required' items.

 

URL to form: http://www.lenderfish.com/brokers/search-test2.php

 

Try a search without selecting the property types (other two selections are required).

 

Anyway, need some assist on this to figure the right way to git 'er done!

 

Thanks!

Link to comment
Share on other sites

Only checked checkboxes are returned to the PHP script. You should always check to see if a an index is defined before using it and use a default value when it's not:

<?php
if (isset($_POST['search'])) {
// query for extracting lenders from amember database
$category = (isset($_POST['catagory']))?strip_tags(trim($_POST['category'])):'';
$loantype = (isset($_POST['loan_type']))?strip_tags(trim($_POST['loan_type'])):'';
$propertytype = (isset($_POST['propertytypes']))?strip_tags(trim($_POST['propertytypes'])):'';

$sql = "SELECT * FROM amember_members WHERE is_lender='Yes' AND '$category' IN (category_1, category_2, category_3, category_4, category_5) AND '$loantype' IN (loan_type, loan_type2, loan_type3, loan_type4, loan_type5) AND '$propertytype' IN (propertytype, property_type2, property_type3, property_type4, property_type5, property_type6, property_type7, property_type8, property_type9, property_type10, property_type11, property_type12, property_type13, property_type14, property_type15, property_type16, property_type17, property_type18, property_type19, property_type20, property_type21, property_type22, property_type23, property_type24, property_type25, property_type27, property_type28) ORDER BY top_3 LIMIT 10";

$results = mysql_query($sql)or die(mysql_error());
$numrows = mysql_num_rows($results);
?>

 

Ken

Link to comment
Share on other sites

A notice isn't an error, it's just php letting you know something.  In this case, as is the case with all "undefined indexes" it means that you are trying to access an element in an array that doesn't exist...

 

$foo = array();
$foo['bar'] = 'a string';

// No notice generated
echo $foo['bar'];

// "Undefined index" Notice generated because 'baz' doesn't exist
echo $foo['baz']

Link to comment
Share on other sites

Gentlemen, as usual..thanks to both of you! I appreciate the explanations so I 'learn'.

 

kenrbnsn, I inserted your changes and that took care of it. My initial hunch was to use the 'isset' method but, again, wanted to get some guidance on the right way. Thanks for confirming that.

 

hitman, thanks for that explanation and analogy. It's good to know this stuff so I can chase the problems down logically myself.

 

Issue solved! :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.