Database Matter
The first step is to open up phpMyAdmin or a database software and incorporate the below query into the SQL section.
The above query will assist the details or information to be stored in the database.
Creating the Form
Now when the database information is now added, let us make the form that will permit the visitors or the end users to perform a search. A file 'index.php' needs to be created.
The form will be using GET instead of POST thereby making the information quite visible in the address bar.
The form is now completed. This will permit us to enter in a query and at the same time will permit the count of results that needs to be shown on the form.
Processing the Query
Create a new file 'search.php' and this is the page where the results from the search will be displayed.
Let us connect to the database first:
Now perform the MySQL query (as a query)
The query mentioned above will look in the page data i.e. the Search results after entering in the keyword and display it as many times as you define it. We will create an array now to display the results.
Let us now just include a link at the bottom that navigates back to the original index page.
Adding Information
We are done with the frontend formation of the search engine. We need to come up with a little form now that would store the details in the database.
Let us create a new page namely 'addurl.php' and create a simple form.
Now create a second page namely 'addurl2.php'. Here the processing of information will take place and added to the database ready to be searched.
Connect to the database
The URL of the page will now get its source code read and made into a variable.
This little line will take away some single quotes that will more often than not disorder the query.
Let us now insert the information into the database.
Give a small added message, just so you know it's been added.
Search Engine is ready!!!
So we are ready with our nice and simple search engine. One can now go to 'addurl.php' and enter the URL(s) of the page(s) that is required to be incorporated into the search engine that you have just created. Assuming you are looking in for ‘e’, there will be good amount of results for the reason that it'll be reading stuff like '<e>'.
We can make things better by following the below mentioned points.
- We can alter the ‘addurl.php’ page that will filter out few of the words or phrases.
- The other important thing is to include pagination and this will permit us to display more than 50 results divided on different pages.
- An admin control panel can be created where we can add, edit and delete the results as well.
- The most important part is the security of the search engine and preventing other people to add information. This can be done by doing a password protects on ‘addurl.php’ and ‘addurl2.php’ pages.
- Addition of the categories such as News, Entertainment, Blogs, technology etc. can be done.
With the completion of this tutorial, we have leant on how to build a HTML form, the connection with the database, the process to read information from a database and obviously how we can add some new information into the database.
Conclusion
The article aimed to provide a simple and easy route to come up with our own Search Engine making use of PHP and SQL language.