Jump to content

[SOLVED] Creating a search...


L

Recommended Posts

Hey,

I'm trying to create a search where a user types a keyword and it searches through the `posts` database...when it finds some matches it echos them..

so I did this:

<?
if (isset($_POST['search'])) {
$key = trim(mysql_real_escape_string(addslashes(strip_tags($_POST['key']))));
$result = mysql_query("SELECT * FROM `posts` WHERE `post`='$key'", $conn1);
while ($results = mysql_fetch_array($result)) {
$threadinfo = mysql_fetch_array(mysql_query("SELECT * FROM `threads` WHERE `id`='".$results['threadid']."' ORDER BY `timestamp`"));
echo "<table><tr class=\"infor\"><td>Thread Name</td><td>Started By</td><td>Date Posted</td></tr><tr><td>".$threadinfo['name']."</td><td>".$results['post']."</td><td>".$results['by']."</td><td>".$results['date']."</td></tr>";
}
}

but when I click the button nothing happens...can someone tell me what's wrong, or if I just need to approach this another way because no single post will be the keyword...how do I make it so it'll choose the post if the keyword is used in it?

 

thanks

 

EDIT: code fixed up a bit

Link to comment
Share on other sites

Use LIKE in your query, so give this a try

 

<?php

if (isset($_POST['search'])) {
    $key = trim(mysql_real_escape_string(addslashes(strip_tags($_POST['key']))));
    $results = mysql_query("SELECT * FROM `posts` WHERE `post` LIKE '%$key%'", $conn1);
    
    while ($threadinfo = mysql_fetch_array($results)) {
        echo "<table><tr class=\"infor\"><td>Thread Name</td><td>Started By</td><td>Date Posted</td></tr><tr><td>"
        .$threadinfo['name']."</td><td>".$results['post']."</td><td>".$results['by']."</td><td>".$results['date']
        ."</td></tr>";
    }
}

?>

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.