Jump to content

form to search DB content not working correctly


webguync

Recommended Posts

I am trying out some code I got from a tutorial and it's not working for me. The code is for a form and when you type in any letter or string that matches against the DB content it is supposed to display. I only get the 'No Matches Found ' string as a result. No DB errors or anything. Also I echoed out the resulting SQL and when I enter that into PHPMyAdmin I come up with results.

 

I am hoping it's just something obvious i am overlooking. Also I am using JQuery and AJAX, but this isn't causing it not to work b/c I tested without the JQuery and still get the same result.

 

code posted below

 

<html> 
<head> 

<script language="javascript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js" />
</script>
<script type='text/javascript'> 
$(document).ready(function(){ 
$("#search_results").slideUp(); 
    $("#search_button").click(function(e){ 
        e.preventDefault(); 
        ajax_search(); 
    }); 
    $("#search_term").keyup(function(e){ 
        e.preventDefault(); 
        ajax_search(); 
    }); 

}); 
function ajax_search(){ 
  $("#search_results").show(); 
  var search_val=$("#search_term").val(); 
  $.post("./find.php", {search_term : search_val}, function(data){
   if (data.length>0){ 
     $("#search_results").html(data); 
   } 
  }) 
} 
</script> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-
8859-1" />

<title>Novo RPC Results Search Engine</title>

<link href="default.css" rel="stylesheet" type="text/css" media="screen" />
</head> 

<body> 
<h1>Search our Phone Directory</h1> 
    <form id="searchform" method="post" action="find.php"> 
<div> 
        <label for="search_term">Search name/phone</label> 
        <input type="text" name="search_term" id="search_term" /> 
<input type="submit" value="search" id="search_button" /> 
</div> 
    </form> 
    <div id="search_results"></div> 
</body> 
</html> 

 

the PHP stuff

<?php
define(HOST, "localhost");
define(USER, "username");
define(PW, "pw");
define(DB, "DB_Name");

$connect = mysql_connect(HOST,USER,PW)
or die('Could not connect to mysql server.' );
mysql_select_db(DB, $connect)
or die('Could not select database.');
$term = strip_tags(substr($_POST['search_term'],0, 100));
$term = mysql_escape_string($term); 
$sql = "select name,phone
from directory
where name like '%$term%'
or phone like '%$term%'
order by name asc";

$string = '';

if (mysql_num_rows($result) > 0){
  while($row = mysql_fetch_object($result)){
    $string .= "<b>".$row->name."</b> - ";
    $string .= $row->phone."</a>";
    $string .= "<br/>\n";
   
  }

}else{

  $string = "No matches found!";

} 

echo $string;
echo $sql;
?>

 

and the DB code

 

CREATE TABLE `directory` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(64) NOT NULL,
  `phone` varchar(16) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;

-- 
-- Dumping data for table `directory`
-- 

INSERT INTO `directory` (`id`, `name`, `phone`) VALUES (1, 'Tom Smith', '512-555-0111'),
(2, 'Bill Smith', '512-555-0112'),
(3, 'John Smith', '512-555-0113'),
(4, 'Jane Smith', '512-555-0114'),
(5, 'Sara Smith', '512-555-0115');

 

 

Link to comment
Share on other sites

I thought this part of the code did that. Guess not huh?

 

if (mysql_num_rows($result) > 0){
  while($row = mysql_fetch_object($result)){
    $string .= "<b>".$row->name."</b> - ";
    $string .= $row->phone."</a>";
    $string .= "<br/>\n";
   
  }

Link to comment
Share on other sites

I thought this part of the code did that. Guess not huh?

 

if (mysql_num_rows($result) > 0){
  while($row = mysql_fetch_object($result)){
    $string .= "".$row->name." - ";
    $string .= $row->phone."";
    $string .= "
\n";
   
  }

You need to execute the query....

$result = mysql_query($sql);  /// This is the execution
if (mysql_num_rows($result) > 0){
  while($row = mysql_fetch_object($result)){
    $string .= "".$row->name." - ";
    $string .= $row->phone."";
    $string .= "
\n";
   
  }

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.