Need help with this PHP script that searches mySQL Database...

Discussion in 'PHP' started by insideout, Jul 21, 2009.

  1. #1
    Hi All...

    I'm working on a website, and one of the pages is an option to search for a particular student. I (modified and) used the following free script I found online, but for some reason, it won't work. I've got two pages- one is the Search form (studentsearch-form.php), and the other is the Search execution code (studentsearch-exec.php).

    The code for studentsearch-form.php is:

    
    <html>
    
    <head>
    <title>Searching for a student...</title>
    </head>
    
    <body bgcolor=#ffffff>
    
    <h2>Search</h2>
    
    <form name="search" method="post" action="studentsearch-exec.php">
    Seach for: <input type="text" name="find" /> in
    <Select NAME="field">
    <Option VALUE="firstname">First Name</option>
    <Option VALUE="lastname">Last Name</option>
    <Option VALUE="idnumber">ID Number</option>
    </Select>
    
    <input type="submit" name="search" value="Search" />
    </form>
    
    </body>
    
    </html>
    
    Code (markup):
    And the code for the studentsearch-exec.php page is:

    
    
    
    <html>
    <head><title>Searching for a student...</title>
    </head>
    <body bgcolor=#ffffff>
    
    <?php
    
    echo "<h2>Search Results:</h2><p>";
    
    //If they did not enter a search term we give them an error
    if ($find == "")
    {
    echo "<p>You forgot to enter a search term!!!";
    exit;
    }
    
    // Otherwise we connect to our Database
    mysql_connect("localhost", "root", "{MyPassword}") or die(mysql_error());
    mysql_select_db("databasename") or die(mysql_error());
    
    // We perform a bit of filtering
    $find = strtoupper($find);
    $find = strip_tags($find);
    $find = trim ($find);
    
    //Now we search for our search term, in the field the user specified
    $data = mysql_query("SELECT * FROM tablename WHERE upper($field) LIKE'%$find%'");
    
    //And we display the results
    while($result = mysql_fetch_array( $data ))
    {
    echo $result['firstname'];
    echo " ";
    echo $result['lastname'];
    echo "<br>";
    echo $result['idnumber'];
    echo "<br>";
    echo "<br>";
    }
    
    //This counts the number or results - and if there wasn't any it gives them a little message explaining that
    $anymatches=mysql_num_rows($data);
    if ($anymatches == 0)
    {
    echo "Sorry, but we can not find an entry to match your query...<br><br>";
    }
    
    //And we remind them what they searched for
    echo "<b>Searched For:</b> " .$find;
    //}
    ?> 
    
    
    </body>
    </html>
    
    
    Code (markup):
    But when I try to execute this, and try out a search term, this is the window that I get:

    I've been sitting on this for a few days now, and I'm falling behind schedule. Any help would be appreciated... also, if, as an alternative, you know of a Search script that works, I would appreciate it if you would provide a link to it or share it with me (if it's free, that is...).

    Thanks in advance... :).

    Tunçay Şanlı
     
    insideout, Jul 21, 2009 IP
  2. kingsoflegend

    kingsoflegend Well-Known Member

    Messages:
    202
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    108
    #2
    The 'find' parameter isn't passed to the search script.

    After <?php write $find=$_POST['find'];
     
    kingsoflegend, Jul 21, 2009 IP