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
https://forums.phpfreaks.com/topic/79765-solved-creating-a-search/
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>";
    }
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.