Jump to content

No results; error on line 15


taichi56

Recommended Posts

Got one problem fixed on this site which is great but now I am not getting any results from my query. I am getting this error from search.php

 

( ! ) Notice: Undefined index: query in C:\wamp\www\search.php on line 15 Call Stack # Time Memory Function Location 1 0.0006 373000 {main}( ) ..\search.php:0

minimum length is 3

 

Here is my code from the index.php

<!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>    
<title>Search</title>    
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form action="search.php" method="GET"> 
<input type="name" name="query" /> 
<input type="submit" value="Search" />
</form>
</body>
</html> 

Here is the search.php code:

<?php  
	mysql_connect("localhost", "root", "") or die("Error connecting to the database: ".mysql_error());
	mysql_select_db("magicgathering") or die(mysql_error());
?>

<!DOCTYPE html Public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR.xhtml/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Search Results</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<?php
    $query = $_GET['query'];
	//Gets value sent over search form
	
	$min_length = 3;
	//you can set minimum length of the query if you want
	
	if(strlen($query) >= $min_length){ // if query length is more or equal.
	
	$query = htmlspecialchars($query);
	// Changes characters used in html to their equivalents, for example: < to &Gt;
	
	$query = mysql_real_escape_string($query);
	// makes sure nobody uses SQL injection
	
	$raw_results = mysql_query("SELECT * FROM tblmtg WHERE ('name' LIKE '%".$query."%') OR ('type' LIKE '%".$query."%')") or die(mysql_error());
	//* means that it selects all fields, you can also write: 'id', 'name', 'type'
	// tblmtg is the name of our table
	
	// '%$query' is what we're looking for , % means anything, for example if $query is Hello 'name'='$query'
	// or if you want to match just full word so "gogohello" is out use '% $query %' ...OR... '$query %' ...OR...  '% $query'
	
	if(mysql_num_rows($raw_results) > 0) { // if one or more rows are returned do following
	
	while($results = mysql_fetch_array($raw_results)) {
		// results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
		
		echo "<p><h3>".$results['name']. "</h3>".$results['type']."</p>";
		// posts results gotten from database (name and type) you can also show id ($results['id'])
	}
	}
	else { // if there is no matchig rows do the following 
		echo "No results";
	}
	}
	else { // if query length is less than minimum 
		echo "minimum length is ".$min_length;
	}
?>
</body>
</html>
Link to comment
Share on other sites

The error is exactly as it says - you are trying to reference an undefined index on this line

 

$query = $_GET['query'];

 

That's because there is no VALID input field in your form with the name 'query'

 

<input type="name" name="query" /> 

 

'name' is not a valid type of input field

Link to comment
Share on other sites

You better wrap "$query = $_GET['query']" inside an if statement to avoid showing errors when search.php is accessed directly.

if(!empty($_GET['query'])) {
 $query = $_GET['query'];
}
else {
 die();
}

I don't think that type="name" has something to do with it being read or not, although I could be wrong.

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.