arjanvr Posted September 15, 2013 Share Posted September 15, 2013 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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/282170-php-search-and-results-same-page/ Share on other sites More sharing options...
jcbones Posted September 15, 2013 Share Posted September 15, 2013 Wrap the database call, and display inside of: if($_SERVER['REQUEST_METHOD'] == 'POST') { //database call and display... } Quote Link to comment https://forums.phpfreaks.com/topic/282170-php-search-and-results-same-page/#findComment-1449579 Share on other sites More sharing options...
arjanvr Posted September 15, 2013 Author Share Posted September 15, 2013 Can you tell me where I should place that piece of code in the script? Quote Link to comment https://forums.phpfreaks.com/topic/282170-php-search-and-results-same-page/#findComment-1449582 Share on other sites More sharing options...
DFulg Posted September 15, 2013 Share Posted September 15, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282170-php-search-and-results-same-page/#findComment-1449616 Share on other sites More sharing options...
arjanvr Posted September 16, 2013 Author Share Posted September 16, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/282170-php-search-and-results-same-page/#findComment-1449658 Share on other sites More sharing options...
DFulg Posted September 16, 2013 Share Posted September 16, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282170-php-search-and-results-same-page/#findComment-1449663 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.