Page 1 of 1

search form

Posted: Mon Jan 19, 2009 11:43 pm
by peterj
Hi,
I am attempting to write a search function for my website.

I have written the below form:

Code: Select all

<form method="post" action="search_result.php" class="searchform">
            <p><label>AD Name: </label><input type="text" name="search" size=25 maxlength=25>
            <input type="Submit" name="Submit" value="Submit"></p>
        </form>
I have written the below php which looks through my mysql database and returns anything with $_POST["search"].
I have written it so that it returns a statement if nothing is entered into the textbox.

What I can't do is, return a statement if someone enters a search term into the textbox but no results are found in the database.
Can someone help?

Code: Select all

<?php
            $search=$_POST["search"];
 
            //get the mysql and store them in $result
            //change whatevertable to the mysql table youre using
            //change whatevercolumn to the column in the table you want to search
            $result = mysql_query("SELECT * FROM products WHERE name LIKE '%$search%'");
 
        if ($search == "") {
                echo "<p class='top_p_margin'>Please enter an AD name to search.</p>";
 
            } else {
 
            //grab all the content
            while($r=mysql_fetch_array($result))
            {
               //the format is $variable = $r["nameofmysqlcolumn"];
               //modify these to match your mysql table columns
 
               $name=$r["name"];
               $email=$r["number"];
               $idnum=$r["idnum"];
               $accmanager=$r["accmanager"];
               $question1=$r["question1"];
               $question2=$r["question2"];
 
 
 
           //display the row
 
                echo "<p class='lbottm'>Name: $name <br/> Email: $email <br/> AD ID Number: $idnum <br/> AD Manager: $accmanager <br/><br/> Question 1: $question1 <br/> Question 2: $question2</p>";
            }
        }
        ?>

Re: search form

Posted: Tue Jan 20, 2009 12:07 am
by requinix
You can use mysql_num_rows to check if there are any rows in the result.