Search form

Hi I am learning PHP on my own and have been going to may site tryin snippets. so far so good. but I ran into an issue. I am now checking out a search feature to browse a mysql DB. I connect just fine but I cannot seem to get it to bring up any results. I am using XAMPP /Apache the DB is New_2_M2 the exiting tables are memeber (Member ID, Member Name), ContactDetails(contactID, memberID{fk}, contactphone, contact email) and product(productid, memberid{fk}, Description, type, price). The data is in there I confirmed that here is the snippet
[php]<?php
/*****************************
New2ME Browse Page
******************************/

$dbHost = ‘localhost’; // localhost will be used in most cases
// set these to your mysql database username and password.
$dbUser = ‘New2Me’;
$dbPass = ‘Ammp123’;
$dbDatabase = ‘New_2_m2’; // the database you put the table into.
$con = mysql_connect($dbHost, $dbUser, $dbPass) or trigger_error("Failed to connect to MySQL Server. Error: " . mysql_error());

mysql_select_db($dbDatabase) or trigger_error("Failed to connect to database {$dbDatabase}. Error: " . mysql_error());

// Set up our error check and result check array
$error = array();
$results = array();

// First check if a form was submitted.

if (isset($_GET[‘search’])) {
$searchTerms = trim($_GET[‘search’]);
$searchTerms = strip_tags($searchTerms); // remove any html/javascript.

if (strlen($searchTerms) < 3) {
$error[] = “Search terms must be longer than 3 characters.”;
}else {
$searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
}

// If there are no errors, lets get the search going.
if (count($error) < 1) {
$searchSQL = "SELECT sid, sbody, stitle, sdescription FROM simple_search WHERE ";

  // grab the search types.
  $types = array();
  $types[] = isset($_GET['body'])?"`sbody` LIKE '%{$searchTermDB}%'":'';
  $types[] = isset($_GET['title'])?"`stitle` LIKE '%{$searchTermDB}%'":'';
  $types[] = isset($_GET['desc'])?"`sdescription` LIKE '%{$searchTermDB}%'":'';
  
  $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)
  
  if (count($types) < 1)
     $types[] = "`sbody` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked
  
      $andOr = isset($_GET['matchall'])?'AND':'OR';
  $searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `stitle`"; // order by title.

  $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");
  
  if (mysql_num_rows($searchResult) < 1) {
     $error[] = "The search term provided {$searchTerms} yielded no results.";
  }else {
     $results = array(); // the result array
     $i = 1;
     while ($row = mysql_fetch_assoc($searchResult)) {
        $results[] = "{$i}: {$row['stitle']}<br />{$row['sdescription']}<br />{$row['sbody']}<br /><br />";
        $i++;
     }
  }

}
}

function removeEmpty($var) {
return (!empty($var));
}
?>

New2Me_Browse #error { color: red; } <?php echo (count($error) > 0)?"The following had errors:
" . implode("
", $error) . "


":""; ?> Search For:
Search In:
Member: /> | Product: /> | Description: />
Match All Selected Fields?

<?php echo (count($results) > 0)?"Your search term: {$searchTerms} returned:

" . implode("", $results):""; ?> [/php]

Hi,

It would be easier for me to check if you provide all the sql files as well.

Regards,
developer.dex2908

Sponsor our Newsletter | Privacy Policy | Terms of Service