Jump to content

how to fix undefined index


pandu

Recommended Posts

Hi, I have an undefined index problem and not sure how to fix it. I dont want to turn off errors - I would like to learn how to actually fix this problem. It happens in line 13 which is marked below with //line 13 comment. The exact error is:

 

Notice: Undefined index: synonymsearch in C:\wamp\www\ezsynonym\index.php on line 13

 

TIA.

 


<?php
//start buffering output
ob_start();

include("db.php");
$mysqli = new mysqli($dbServer, $dbUser, $dbPass, $dbName);
//report any errors on screen for debugging
//echo "connection errors: " . mysql_error() . "<BR>";

//STORE THE USER INPUT IN VARIABLES
$strErrorResults = "";
$strWordResults = "";
//line 13 - this is where the error happens. 
$queryWord = $_REQUEST['synonymsearch'];

if ($queryWord == "" || $queryWord == "Search Synonym") {
		$strWordResults = "Enter a word to search for";
}
else {			//search for target word in wordlist
		$result = $mysqli->query("select * from wordlist where word='{$queryWord}'");			
		if ($result->num_rows > 0) 
			{ //extract the target word's primary key 
					$word_arr = $result->fetch_array(MYSQLI_ASSOC);
					$word_id = $word_arr['pri'];				
			  //search for target word's primary key in the links table's wordid column
					$synonymQuery = "select * from links where wordid='{$word_id}'";
					$synonymResult = $mysqli->query($synonymQuery);			
					$arrKeys = array();										//create an array


					if ($synonymResult->num_rows > 0) {				//grab the corresponding synonym ids and store into the array
						while ($row = $synonymResult->fetch_array(MYSQLI_ASSOC)) { 
							$arrKeys[] = $row['synid'];
							}
						}

					foreach ($arrKeys as $value) {							
						$sQuery = "select * from wordlist where pri='{$value}'";
						$result = $mysqli->query($sQuery);
						$result_arr = $result->fetch_array(MYSQLI_ASSOC);
				//		echo $result_arr['word'];
						$strWordResults .= <<<END

			<div id="results-left"><a href="index.php?synonymsearch={$result_arr['word']}"> {$result_arr['word']} </a></div><br/><br/>
END;
						} // end for
		} // end if
		else {
					//word does not exist in the database
$strErrorResults .= <<<END

	<p>failure... word does not exist in the system</p>
END;
		}
}

print <<<END

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="style.css" type="text/css" media="screen"> 
<link rel="icon" href="favicon.ico" type="image/x-icon"> 

<title>ezSyonym</title>
</head>
<body>

    <div id="header">
    	<a href="index.php"><img src="images/logo-final.gif"></a>
</div>

    
    <div id="content">
    	<div id="left-col">
          	<div id="searchbox">
                <form id="searchform" method="post" action="index.php">
                <input id="input" name="synonymsearch" type="text" value="Search Synonym" maxlength="400px" />
                </form>
            </div>
            <div id="search-results">
				{$strErrorResults}
				{$strWordResults}
            </div>
        </div>
                
        <div id="right-col">
        	<div id="sidebar">     
            </div>        
        </div>        
    </div>
    
    <div id="footer"></div>

</body>
</html>

END;

//send data to the browser
ob_end_flush();

?>

Link to comment
https://forums.phpfreaks.com/topic/221420-how-to-fix-undefined-index/
Share on other sites

yes, that is better if you want to cover "is set" and "isn't empty" in the same shot. in a form post situation, i typically check isset for all vars, then validate each one separately. sometimes it's okay if some of them are empty so a blanket check for empty() wouldn't work.

Archived

This topic is now archived and is closed to further replies.

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