Forum Moderators: coopster

Message Too Old, No Replies

Php and mysql search engine

         

tsiwu

2:36 pm on Jan 13, 2009 (gmt 0)

10+ Year Member



hi guys can you please help me out, im trying to create a search engine for my website, what i must admit is that i am a novice coder and therefore do not know much! below is the code that i have used! Can someone please help me out or give me advice on some other code that i can use! Thank you!

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]

coopster

7:47 pm on Jan 14, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Can someone please help me out

What exactly is the problem?

simstar

8:00 pm on Jan 14, 2009 (gmt 0)

10+ Year Member



Thats a very basic 'search engine' there are free ones available on the net but they are of similar type to what you have created. I don't see why the basic structure (I scanned the code quickly) would not work.

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.

henry0

9:06 pm on Jan 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your search is in FULLTEXT mode.
Perhaps you have not set FULLTEXT on the cols you try to query?
Also be advised that if I am correct you may add a fulltext index on only 16 cols