Jump to content

php search and results same page


arjanvr

Recommended Posts

Hello,

 

I want to use a search script on my page that searches trough a database and shows the results on the same page. I did place it all in one page to test but when I open the page it shows the entire database. Since my database will end up with 15000+ entries it's going to cause a problem. When I search it does show the right results on the page but how can I start off with a a empty page? In the end I want a search script where you can select from diffent criteria.

<?

echo "    <p><b>Zoek in de database:</b></p>\n"; 
echo "    \n"; 
echo "    <p>\n"; 
echo "    <form method=\"post\" action=\"resultaat.php\">\n"; 
echo "    Zoekterm:<br>\n"; 
echo "    <input type=\"text\" name=\"zoekterm\" size=\"30\"><br>\n"; 
echo "    <input type=\"submit\" name=\"submit\" value=\"Zoeken\">\n"; 
echo "    </p>\n";


    //-- natuurlijk moet er eerst een connectie met de database worden gemaakt:
    if (!@mysql_select_db("schoolme_scholen", @mysql_connect("localhost", "schoolme_arjan", "avr2nl3")))
    {
        echo "Er kan geen database connectie gemaakt worden.";
        exit();
    }

    //-- $_POST['zoekterm'] is de naam van het zoekveld in het formulier wat we
    //-- hebben gemaakt in het vorige 'hoofdstuk'. Dit is dus de SQL code:
    $sql = "SELECT id,naam,type,geloof,adres,postcode,plaats,provincie,telefoon,fax,email,website FROM scholen WHERE plaats LIKE '%" . mysql_real_escape_string($_POST['zoekterm']) . "%'";
    
    //-- voer de SQL code uit en zet dit in een variabele zodat we zometeen kunnen
    //-- kijken of er een resultaat is
    $res = mysql_query($sql);
    
    //-- bekijk nu of er een resultaat is, of het zoekwoord dus gevonden is of niet
    if (mysql_num_rows($res) >= 1)
    {
        //-- er is een resultaat gevonden, toon de resultaten via een while () loop
        while ($row = mysql_fetch_array($res))
        {
echo "<table width=\"100%\" border=\"5\">\n"; 
echo "  <tr>\n"; 
echo "    <td width=\"50%\"><b>id:</b> $row[id] </td>\n"; 
echo "    <td width=\"50%\"><b>Naam:</b> $row[naam] </td>\n"; 
echo "  </tr>\n"; 
echo "  <tr>\n"; 
echo "    <td><b>Type:</b> $row[type] </td>\n"; 
echo "    <td><b>Geloof:</b> $row[geloof] </td>\n"; 
echo "  </tr>\n"; 
echo "  <tr>\n"; 
echo "    <td><b>Adres:</b> $row[adres] </td>\n"; 
echo "    <td><b>Postcode:</b> $row[postcode] </td>\n"; 
echo "  </tr>\n"; 
echo "  <tr>\n"; 
echo "    <td><b>Plaats:</b> $row[plaats] </td>\n"; 
echo "    <td><b>Provincie:</b> $row[provincie] </td>\n"; 
echo "  </tr>\n"; 
echo "  <tr>\n"; 
echo "    <td><b>Telefoon:</b> $row[telefoon] </td>\n"; 
echo "    <td><b>Fax:</b> $row[fax] </td>\n"; 
echo "  </tr>\n"; 
echo "  <tr>\n"; 
echo "    <td><b>E-mail:</b> $row[email] </td>\n"; 
echo "    <td><b>Website:</b> $row[website] </td>\n"; 
echo "  </tr>\n"; 
echo "</table>\n";
echo "  <p>\n";
        }
        
        echo '<a href="zoek.php" title="zoek opnieuw">zoek opnieuw</a>';
    }
    //-- als er geen resultaat is gevonden, dus als het zoekwoord niet gevonden is:
    else
    {
        echo '<p>Er is niets gevonden op jou zoekterm: <b>' . $_POST['zoekterm'] . '</b></p>';
        
        echo '<p><a href="zoek.php" title="zoek opnieuw">zoek opnieuw</a></p>';
    }
?>
Link to comment
Share on other sites

1. Don't be afraid to use both HTML and PHP in tandem, right now you have spaghetti code.

 

2. Using the error suppression operator (@) is a bad practice as it promotes sloppy coding without the proper error handling.

 

3. It's a good practice to store your database connection in a variable for proper error handling instead of invoking it inside of another function. 

 

4. I recommend against using MYSQL as it has been deprecated and will no longer be used in a future version of PHP. I recommend using either MYQSLi or PDO.

Link to comment
Share on other sites

So what you are saying is should rewrite the entire script? I am still learning php, not 18 so it could take a bit longer so for now I am trying to grab scripts that are offered on the internet and learn from that also..

 

Will a 15k 8 row database cause problems in terms of size when using a search script or in general?

Link to comment
Share on other sites

Typically with a search script, you will only display a certain amount of rows using the LIKE keyword and use pagination to navigate to the next set of rows.

$sql = "SELECT field FROM table WHERE field = 'value' LIMIT 0,10";

There is a tutorial on pagination on this site I believe, although I'm not sure exactly where I saw it.

 

Do not use $_SERVER['PHP_SELF'] as a forms action, as this is a security risk because the value can be tampered with. Passing the action attribute an empty string is fine.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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