1

I am receiving the following error from my script.

[Tue Dec 01 09:17:10 2015] [error] [client xxxx] PHP Notice:  Undefined index: search in xxx/search.php on line 3, referer: http://xx/

Additionally I get the following error sometimes as well

PHP Notice:  Undefined offset: 4 in xxx/search.php on line 21, referer: http://xxx/search.php?search=C8053A-B&submit=Search

The code for search.php is

<?php
// Words 
$words = trim($_GET["search"], ' ');
echo '<br>';
echo '<div align="center">';
echo '<form  method="get" action="search.php?"  id="searchform">';
echo '<input  type="text" name="search">';
echo '<input  type="submit" name="submit" value="Search">';
echo '</form>';
echo '</div>';
if ($words != '')
{
echo 'Search Results for <b>'.$words.'</b>:';
$file  = fopen('inv.csv', 'r');
$crossw = $words;
$words = array($words);   
$words = array_map('preg_quote', $words);
$regex = '/'.implode('|', $words).'/i';
print('<table border="1"><tr><td width="150">Primary Part #</td><td    width="75">Price</td><td width="75">Qty Avail</td><td width="105">Searched #</td><td width="375">Description</td></tr></table>');
while (($line = fgetcsv($file)) !== FALSE) {  
list($part, $price, $qty, $cross, $desc) = $line;
if(preg_match($regex, $cross)) {
    print('<table border="1"><tr><td width="150">'.$part.'</td><td width="75">'.$price.'</td><td width="75">'.$qty.'</td><td width="105">'.$crossw.'</td><td width="375">'.$desc.'</td></tr></table>');
}
}
if ($crossw != '')
{
echo 'End of results.';
}}
?>

I want to get rid of the error without turning off logging in my php.ini.

What is the source/reason for the error?

Community
  • 1
  • 1
Jesse
  • 33
  • 3
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Sean Dec 01 '15 at 15:36
  • 1
    `if(isset($_GET["search"]))` is your friend – Sean Dec 01 '15 at 15:37
  • It might be you are not accessing the page with the query string search eg: `http://yourpage.com/?search=blabla` as Sean mention `isset` is the way to go :) – Kheshav Sewnundun Dec 01 '15 at 15:42

1 Answers1

1

This notice invites you to take into account the possibility that the search url parameter is not always set (perhaps when you first browse that search page ?).

$words = trim($_GET["search"], ' ');

You're assuming here that the ̀$_GET["search"] variable is always available for you to use, which is not the case.

This case should be properly handled in php with an isset() construct with a ternary operator (shorthand for if/else statement) like so :

$words = isset($_GET["search"])?trim($_GET["search"], ' '):''; // $words will be set to an empty string in case the search param is not set.
Calimero
  • 4,238
  • 1
  • 23
  • 34
  • Thank you this fixed it. As I said I am a complete newbie but trying to learn! – Jesse Dec 01 '15 at 15:49
  • you're welcome, please consider accepting the answer. – Calimero Dec 01 '15 at 15:51
  • Will do as soon as I find where to do so! Any thoughts on the undefined offset error? – Jesse Dec 01 '15 at 17:14
  • Sorry I had not seen that part of your question before. It seems in some lines of your csv file you only have 4 fields instead of 5 (as expected by the `list()` construct on line 21), thus this notice. I'd suggest manually editing the file if possible. – Calimero Dec 01 '15 at 17:26
  • OK I need to find a way to work around that as some items have prices and some do not. Thank you! – Jesse Dec 01 '15 at 18:51
  • might be a little trickier than the former issue (I imagine something like `if(count($line)==4) {...} else {...}` might just do it), don't hesitate to ask another question if you struggle with this, I might just help if I come across :) gl ! – Calimero Dec 01 '15 at 20:23