Jump to content

create a search function for my website


dazz_club

Recommended Posts

Hi guys and girls,

 

I am working on a project and I would like to give the user the facility to search for a product on my website.

 

If anyone has any urls to good tutorials then that would be great.

 

Here is the html code for my form

<form name="form1"  id="form1" method="POST" action="searchDataBase.php">
					<ul style="list-style-type:none;">
					<li><input type="text" name="searchterm" id="search" ></li>
					<li><input type="image"  name="submit" value="submit!" src="images/search-bttn.png" id="submit"  title="Click here to search"></li>
					</form>

 

and here is the scripti have for searchDataBase.php

<?php require_once("includes/connection.php"); ?>
<?php
$query= "select * from products where $searchtype like '%$searchterm%'";
$reslut = mysqli_query($db, $query);

?>

 

Below is how i have structured my products table

 

products

----------------

product_id

name

type

image

category

-----------

 

I have about 26 products in this table so far.

 

Kind regards

Dazzclub

Link to comment
Share on other sites

It looks like your off to the right start. Change the searchDataBase.php to this:

 

<?php 
require_once("includes/connection.php"); 

$searchterm = mysql_real_escape_string($_POST['searchterm']);

//I don't see an input in your form named "searchtype", So I'm not sure where this is coming from
$searchtype = mysql_real_escape_string($_POST['searchtype']);

$query= "SELECT * FROM products WHERE $searchtype LIKE '%$searchterm%'";
$result = mysqli_query($db, $query)or die(mysql_error() . "<p>With query:<br>$query");

while ($row = mysql_fetch_assoc($result)){
   echo $row['name'].'<br>';
}

?>

 

I put a comment in the code, I wasn't sure where you pulling a variable from.

Link to comment
Share on other sites

This works well:

 

<?php
$query_count = "SELECT SQL_CALC_FOUND_ROWS
  title,
  content,
  URL,
  id
FROM $searchType
  WHERE
    MATCH(URL,title,content) AGAINST ('$searchQuery' IN BOOLEAN MODE) AND
content IS NOT NULL
LIMIT $limitvalue, $limit";

$sql = mysql_query($query_count)or die(mysql_error()); 
$result_count = mysql_query("SELECT FOUND_ROWS()")or die(mysql_error());
$total = mysql_fetch_array($result_count);
$totalrows = $total[0];   // returns ALL found rows if LIMIT wasn't there
?>

 

Replace the columns and tables as yours.

Link to comment
Share on other sites

@pocobueno1388

 

Thanks for the reply, and thanks for the little bit of incouragement i thought i was goin to some harsh feedback in regards to what i have wrttin so far, code wise.

 

That variable you questioned i believe was left there by accident, originally i had to input fields but i thought it would be best to have one.

 

I have ran the script

 

and this message is being displayed

code of html form

<form name="form1"  id="form1" method="POST" action="searchDataBase.php">
<ul style="list-style-type:none;">
<li><input type="text" name="searchterm" id="search" ></li>
<li><input type="image"  name="submit" value="submit" src="images/search-bttn.png" id="submit"  title="Click here to search"></li>
</form>

 

searchDataBase.php script


<?php 
require_once("includes/connection.php"); 

$searchterm = mysql_real_escape_string($_POST['searchterm']);

$query= "SELECT * FROM 'products' WHERE $searchterm LIKE '%$searchterm%'";
$result = mysqli_query($db, $query)or die(mysql_error() . "<p>With query:<br>$query");

while ($row = mysql_fetch_assoc($result)){
   echo $row['name'].'<br>';
}

?>

 

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in E:\wamp\www\drinkpromo\searchDataBase.php on line 7

With query:
SELECT * FROM products WHERE LIKE '%dazzclub%'

 

What have i done wrong???

Link to comment
Share on other sites

Hi nethnet,

 

I have now ammended the form so that the additional field no longer exists.

 

my new code now looks like. (searchDataBase.php)

<?php 
require_once("includes/connection.php"); 

$search = mysql_real_escape_string($_POST['search']);

$query= "SELECT * FROM 'products' WHERE $search LIKE '%$searchterm%'";
$result = mysqli_query($db, $query)or die(mysql_error() . "<p>With query:<br>$query");

while ($row = mysql_fetch_assoc($result)){
   echo $row['name'].'<br>';
}

?>

 

and the form which is submitting the searchterm is

<form name="form1"  id="form1" method="POST" action="searchDataBase.php">
<ul style="list-style-type:none;">
<li><input type="text" name="search" id="search" ></li>
<li><input type="image"  name="submit" value="submit" src="images/search-bttn.png" id="submit"  title="Click here to search"></li>
</form>

 

Hope this helps

Link to comment
Share on other sites

I'm confused by your script.  Your form is submitted to searchDataBase.php and the search query from the form is set to the variable $search.  So what exactly are your users searching through?  From the looks of it, you're allowing your users to search through your database structure.  In your SQL:

 

"SELECT * FROM 'products' WHERE $search LIKE '%$searchterm%'"

 

There are a few errors.  If your customers are searching for a product name, for example, you'd need to rearrange your SQL a little bit:

 

"SELECT * FROM `products` WHERE `name` LIKE '%$search%'"

 

That way, the $search variable is used to search for the actual name of the product, and not for a column in your database called '$search'.  This is, of course, assuming you have a column named 'name' with the products name stored in it.

 

Theo

Link to comment
Share on other sites

hmm sorry for all the confusion

 

ok, hopefully i can explain it better.

 

i have search form, when works, will pass the variable search to searchDatabase.php which will then, using the chosen search term, go through the products table and out put all the products that  matche the users term.

 

does this help?

 

sorry if i am annoying you.

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.