SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question PHP Script to search MySQL Database- Need Help...

    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:

    Code:
    <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>
    And the code for the studentsearch-exec.php page is:

    Code:
    
    <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>
    But when I try to execute this, and try out a search term, this is the window that I get:

    Search Results:

    Notice: Undefined variable: find in C:\wamp\www\SWD\studentsearch-exec.php on line 15


    You forgot to enter a search term!!!
    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 Sanli

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Location
    Austria
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Add:
    $find=$_POST['find'];
    at studentsearch-exec.php.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would also avoid the use of inline PHP within HTML as you have there.

    I always put an include at the top of HTML pages, and then put as much PHP as I can in the include file (abit like a code file). Obviously when you are displaying information on screen you need code within HTML.

  4. #4
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb

    Magic!!! . That was perfect, blubb... works great now. Thank you so much... .

    And littlened, thanks for the tip. I'll check online for the syntax to use "include", and try to incorporate it into the code...

    Cheers ...

    Tunšay Sanli


Tags for this Thread

Bookmarks

Posting Permissions

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