4 Replies - 782 Views - Last Post: 04 March 2012 - 02:17 PM Rate Topic: -----

#1 hathead1990  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 25-November 11

PHP NOOB needs help with PHP search!

Posted 04 March 2012 - 12:33 PM

Hi all im a newbie to this forum and need help with creating a search for my site!

I am trying to create a search dependent on what title, artist, or genre is entered.

I keep receiving these errors. Thanks in advance.

Notice: Undefined variable: search_query in H:\Other\Easy PHP\EasyPHP-5.3.9\www\Mobile Application\checksearch.php on line 7

Deprecated: Function eregi() is deprecated in H:\Other\Easy PHP\EasyPHP-5.3.9\www\Mobile Application\checksearch.php on line 7
Error: you have entered an invalid query, you can only use characters!


MY CODE BELOW (Attachment Added)

Attached File  checksearch.php (1.03K)
Number of downloads: 14

<?php session_start();?>

<?php 

include 'include/db_config.php';

if (!eregi("[[:alpha:]]", $search_query)) 

{ 

echo "Error: you have entered an invalid query, you can only use characters!<br>"; 

exit; //No need to execute the rest of the script. 

} 

$query= mysql_query("SELECT * FROM products WHERE title='$search_query' 

OR artist='$search_query' OR genre='$search_query' ORDER BY genre"); 

$result= mysql_numrows($query); 

if ($result == 0) 

{ 

echo "Sorry, no matches found ($search_query)"; 

exit; //No results found, why bother executing the rest of the script? 

} 

else if ($result == 1) 

{ 

echo "I've found <b>1</b> match!<br>"; 

} 

else { 

echo "I've found <b>$result</b> matches!<br>"; 

} 

while ($row= mysql_fetch_array($query)) 

{ 

$title= $row["title"]; 

$artist = $row["artist"]; 

$genre = $row["genre"]; 



echo "The title is: $title.<br>"; 

echo "The artist is: $artist.<br>"; 

echo "The genre is: $genre.<br>"; 

} 

?> 

</body> 

</html> 



Is This A Good Question/Topic? 0
  • +

Replies To: PHP NOOB needs help with PHP search!

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: PHP NOOB needs help with PHP search!

Posted 04 March 2012 - 12:48 PM

Just like it says:

Quote

Undefined variable: search_query in H:\Other\Easy PHP\EasyPHP-5.3.9\www\Mobile Application\checksearch.php on line 7


line 7:

if (!eregi("[[:alpha:]]", $search_query)) 


Where, pray tell, does the variable $search_query come from? I'm guessing you're copying and pasting this code from somewhere without having a clue how it works?

Like maybe this one? I see this code all over the net, so who knows. That crap post I just linked to apparently depends on register_globals being set to on, which it is usually not (nor should it be...it's a security risk).
Was This Post Helpful? 0
  • +
  • -

#3 hathead1990  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 25-November 11

Re: PHP NOOB needs help with PHP search!

Posted 04 March 2012 - 12:56 PM

Im sorry but I created the code myself obviously with some help online yes!

Quote

Where, pray tell, does the variable $search_query come from?


The search_query comes from a form on another page. Here is the code


<form action="checksearch.php" method="post">
<input name="search_query" type="text" value="E.G. ROCK" size="15" maxlength="25">
<input type="submit" name="submit" value="Submit">
</form>



Was This Post Helpful? 0
  • +
  • -

#4 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: PHP NOOB needs help with PHP search!

Posted 04 March 2012 - 01:47 PM

I don't think you understand forms.

When a form is "sent", it's variables are being sent. <input name="search_query"> does NOT become $search_query when sent. Instead, it will be sent via GET, where all of the variables are put in the URL (page.php?variable=value&variable2=value2) or post, which will hide the variables. You can retrieve these variables with the two variables, $_GET and $_POST. I guess you can guess which one to use.

So to fix your first problem, add $search_query = $_POST['search_query']; before you used $search_query.
Was This Post Helpful? 1
  • +
  • -

#5 hathead1990  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 25-November 11

Re: PHP NOOB needs help with PHP search!

Posted 04 March 2012 - 02:17 PM

Thankyou for your reply :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1