batsta13
batsta13

Reputation: 539

PHP/MySQL unidentified index error

I am working on a PHP assignment which requires me to create a search form that searches a databases with the criteria input by the user. In the MySQL database I have a firstName and lastName columns. To make it easier for the user I wanted to the concatenate the to and input it in a listbox so the user could click the person name however I am now getting an undefined index error. I have researched for a solution but haven't been able to find one.

I am connecting to the server and the database elsewhere in the file. I have shown only the code relevant to the problem below.

Form code:

<form action="search.php" method="POST">
    <fieldset style="background-color:#CCC">
    <p> person: <?php fullname(); ?><br>
    </fieldset>
</form>

function fullname()
{
    $query = 'SELECT CONCAT (firstName,\' \',lastName) FROM athlete ORDER BY lastName';
    $result = mysql_query($query);
    echo("<select name=\"fullnameListbox\">");
    echo("<option value=\"empty\"></option>"); 
    while($row = mysql_fetch_assoc($result))
    {
        foreach($row as $index=>$value)
        {
            echo("<option value=\"$value\"> $value </option>"); 
        }
    }
    echo("</select>");
}

The processing form:

echo($_POST['fullnameListbox']);

I repeat the error I am getting an unidentified index error. Any help is appreciated.

I inlcluded print_r($_POST) as suggested below and i get the following:

( [lastNameListbox] => empty [lastNameText] => [search] => Submit )

I take it that lastNameListBox is empty even though I have clicked a name in the listbox so theoretically it shouldn't be empty.

Upvotes: 0

Views: 605

Answers (2)

Mihai Stancu
Mihai Stancu

Reputation: 16107

An unidentified index notice is a PHP notice relating to trying to access array indexes that were not initialized yet.

$_GET['some_key_name']++; // will result in such a notice
                          // if the array key was never initialised

if(isset($_GET['some_key_name'])) // will not result in such a notice
    $_GET['some_key_name']++;     // will not result in such a notice
else
    $_GET['some_key_name'] = 1;   // will not result in such a notice

Upvotes: 1

Basith
Basith

Reputation: 1075

Hi first you have to connect database then only you can execute the query as well.

<?php
   $con = mysql_connect("localhost","peter","abc123");
   if (!$con)
   {
       die('Could not connect: ' . mysql_error());
   }
 // some code
 mysql_close($con);
 ?>

Try this..

Upvotes: 0

Related Questions