[RESOLVED] Search Box Code
Results 1 to 5 of 5

Thread: [RESOLVED] Search Box Code

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    60

    resolved [RESOLVED] Search Box Code

    Hi This is the php code I used:

    PHP Code:
    <html> 
    <head><title>processform</title> 
    </head> 
    <body> 
    <?php 

    // include MySQL-processing classes 

    $var = @$_GET['q']; 
    $trimmed trim($var); 
    // connect to MySQL 

    $dbhost "db.db.xxxxxxx.abc.com"
    $dbname "dbname"
    $dbuser "dbuser"
    $dbpass "password"

    $link mysql_connect($dbhost$dbuser$dbpass); 
    if (!
    $link) { 
    die(
    'Could not connect: ' mysql_error()); 


    $db_selected mysql_select_db('dbname'); 
    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, year, Genre, TypeOfDisc, Price, image 
    FROM searches WHERE MATCH(ArtistName) AGAINST ('
    $keyword' IN BOOLEAN MODE) 
    LIMIT 0 , 30"

    $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>
    This is the HTML Code I used:
    PHP Code:
    <form method="get" action="processform.php"


    <
    div style="display:inline-block; width: 270px; overflow:hidden;"></div
    <
    input name="searchterm" type="text"  size="55" class="searchbox" title="What service are you looking for ?" value="What service are you looking for ?" width="445px" /> 


    <
    input type="submit" name="search" title="Search Now! 
    "
    value="Search" class="searchbutton" /> </div

    </
    form
    It gives me the error message that is in the Code " No results were found, Go back and try a new search."

    However, it gives the same message no matter what I search for, even for words I know are in the DB.

    Can someone please help

    Thanks in advance

  2. #2
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    1,783
    Well if there is only one result for each search term you've tried the reason you don't see it would be because you use up the first row int he result set with this line:
    PHP Code:
    $row_sql mysql_fetch_assoc($query); 
    Then later try to use the remaining rows (which won't exist if only one row was returned) in a loop.
    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

  3. #3
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,428
    You should set error_reporting to E_ALL and either display_errors or log_errors to On. You should be getting PHP error messages about undefined variables, such as on this line:

    PHP Code:
    $keyword mysql_real_escape_string($_POST['keyword']); 
    According to the HTML code you posted, $_POST['keyword'] will never exist since your form is using the GET method - not POST.

  4. #4
    Member
    Join Date
    Sep 2011
    Posts
    60

    Smile New Simplified Code - still Error - Please Help

    I have 2 files Search and Search Result.

    This is the code for search.php
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
       <body>

    <?php include("header.php"); ?>


    <style type="text/css">
    .image {
       position: relative;
       width: 100%; /* for IE 6 */
    }

    h2 {
       position: absolute;
       top: -28px;
       float:right
       left: 0;
       right:10;
       width: 100%;
       font: myriadpro;
    }


    text{
       position: absolute;
       top: 36px;
       float:right
       left: 60;
       right:40;
       width: 100%;
       font: myriadpro;
    }


    </style>

    <!-- END HEADER --->






    <div class="image" width:3000;>

     <img src="images/contactusbox.png" width="1100" height="470"/>

          




    <br/>


    <text>
    <table align="center" border=0 width=350 style='table-layout:fixed'>
        <col width=350>
            <tr>
                <td>
                    <br /><br />
                        <form action="searchresult.php" method="post" id="contact" name="contact" onsubmit="return validateForm();" >
                            <table align="center">
                                   <tr>
                                        <td>
                                            <label for="fname">Search Term*</label>
                                        </td>
                                        <td colspan="2" id="fname">
                                            <input type="text" name="fname" value="" id="fname" /> 
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colspan="2" id="sub">
                                            <input type="submit" name="submitb" value="Search" id="submitb" /> 
                                            <input type="reset" name="resetb" value="Reset" />
                                        </td>
                                    </tr>
                            </table>
    </table>


       




    <!-- FOOTER BEGINS -->


    <?php include("footer.php");?>
    and this is the code for searchresult.php
    PHP Code:
    <?php 
    require_once 'config.php';require_once 'contactemail.php';
    $ip $_SERVER['REMOTE_ADDR'];


    function 
    esc($data) {
        return 
    mysql_real_escape_string($data);
    }


    function 
    getesc($varname) {
        global $
    $varname;
        $
    $varname = isset($_POST[$varname])    ? esc($_POST[$varname])    : "";
        return $
    $varname;
    }

    //Get data in local variable

    getesc('fname');

     

    // check local variables for avialbilty in database

     
    $query = ("select * from contact where fname='.$fname'"); 
    $res mysql_query($query);
    if(
    mysql_num_rows($res) == 1);
    echo 
    $res;

    ?>




    <?php
    include "footer.php";
    ?>
    Now I have no error message but all tht I get in result is
    PHP Code:
     Resource id #2 
    Can someone please help

  5. #5
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    that's what $res is - a mysql result resource.

    if you're trying to see the result of your query, you need to access them (not echo the resource). Maybe mysql_fetch_assoc() is what you're looking for?

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

    what are you trying to do, here, exactly?
    PHP Code:
    function esc($data) { 
        return 
    mysql_real_escape_string($data); 


    function 
    getesc($varname) { 
        global $
    $varname
        $
    $varname = isset($_POST[$varname])    ? esc($_POST[$varname])    : ""
        return $
    $varname


    //Get data in local variable  // what? you're in the global scope

    getesc('fname'); 
    is this what you're trying to do?
    PHP Code:
    $fname mysql_real_escape_string$_POST['fname'] ); 
    -----------------

    add this to your code, after $query = ("select * from contact where fname='.$fname'");
    PHP Code:
    print $query
    Last edited by traq; 04-16-2012 at 12:41 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •