Forum Moderators: coopster
Processform.php
<html>
<head><title>processform</title>
</head>
<body>
<?php
// include MySQL-processing classes
$var = @$_GET['q'];
$trimmed = trim($var);
// connect to MySQL
$dbhost = "localhost";
$dbname = "musicwebsite";
$dbuser = "";
$dbpass = "";
$link = mysql_connect('localhost', '', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('musicwebsite');
if (!$db_selected) {
die('Could not select database: ' . mysql_error());
}
$keyword = mysql_real_escape_string($_POST['keyword']);
// Perform the fulltext search
$sql = "SELECT productID, ArtistName, AlbumName, year, Genre, TypeOfDisc, Price, image
FROM music WHERE MATCH(ArtistName) AGAINST ('$keyword')";
$query = mysql_query($sql) or die(mysql_error());
$row_sql = mysql_fetch_assoc($query);
$total = mysql_num_rows($query);
if($total>0) {
while ($row_sql = mysql_fetch_assoc($query)) {//echo out the results
echo ''.$row_sql['ArtistName'].'<br />'.$row_sql['Price'].'';
}
} else
{
echo '<div class="maincontainer"><h2>No results were found, Go
back and try a new search.</h2></div>'.'';
}
echo '</div>';
?>
</body>
</html>
Search engine code that i paste on every page that i want to have the search text box
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-
8859-1">
<title>MySQL-based Search Engine</title>
<link href="default.css" rel="stylesheet" type="text/css" media="screen">
<script language="javascript" type="text/javascript">
window.onload=function(){
if(document.getElementById&&document.
getElementsByTagName&&document.createElement){
var sfield=document.getElementsByTagName('form')[0].elements[0];
if(!sfield){return};
sfield.onfocus=function(){this.value=''};
sfield.onblur=function(){
if(!this.value){this.value='Enter your search term here'};
}
}
}
</script>
</head>
<body>
<h1>Music2Movies Search Engine</h1>
<div class="maincontainer">
<form method="get" action="processform.php">
<input name="searchterm" title="Enter your search
term here" value="Enter your search term here" class="searchbox" type="text">
<input name="search" title="Search Now!" value="Search" class="searchbutton" type="submit">
</form>
</div>
</body>
</html>
[edited by: eelixduppy at 3:57 pm (utc) on Jan. 13, 2009]
[edit reason] removed db specifics [/edit]
User enters keyword into form -> your file queries against it and returns the results?
What do you need help with?
It seems ok for a simple solution. You have also used real escape which is good for security. Make sure there are no other loose ends which can be hacked.