Jump to content

taichi56

Members
  • Posts

    4
  • Joined

  • Last visited

taichi56's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. 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>
  2. Oh, so simple. Thank you to both of you.
  3. Not sure what you are saying. Sorry. I am a newbie.
  4. I recieved this coding on another site and changed it a little to get it to work with MYSQL database. I am able to connect to my localhost database but when I put in a search term I get this error: ! ) Notice: Undefined variable: Get in C:\wamp\www\search.php on line 15 Call Stack # Time Memory Function Location 1 0.0003 373624 {main}( ) ..\search.php:0 minimum length is 3 The error is showing on line 15: $query = $_GET['query'] Here is my 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>
×
×
  • 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.