Employee Search and Display System
Employee Search and Display System
To enhance security, prepared statements and parameterized queries should be used instead of executing raw SQL queries directly. This provides a more robust protection against SQL injection. Additionally, measures such as input validation and use of a web application firewall could be implemented for comprehensive security .
The 'select_db' method specifies which database to use for the queries executed through the connection. Although it's included in the script, it is technically unnecessary in this context because the database is already specified during the initial connection setup with MySQLi .
Using the 'GET' method allows parameters to be passed in the URL, which can be useful for simple requests. However, it exposes query data in the browser's address bar, which could lead to data leakage and limit the amount of data sent due to URL length restrictions; sensitive information and large payloads should be handled with POST requests instead .
The user interface dynamically updates as the user types in the input field by attaching an 'onkeyup' event listener to the input element. This triggers the 'display()' function, which executes an asynchronous XMLHttpRequest to fetch and display the matching employee records in real-time, updating the DOM element with the ID 'result' with the server's response .
The JavaScript function uses XMLHttpRequest to asynchronously send a GET request to 'Employee.php'. It checks the readyState and status to update the web page with the server response only when the request is complete and successful, thus ensuring that the page does not need to refresh to display the data .
Omitting 'real_escape_string' would leave the application vulnerable to SQL injection, where an attacker could manipulate SQL queries by entering malicious input, potentially compromising the entire database by executing unauthorized queries .
The PHP script handles a database connection error by using the 'connect_error' property of the MySQLi object. If a connection error occurs, it outputs a message using 'die()', which stops the script execution and displays the error .
The PHP script uses the 'real_escape_string' method to sanitize the user input, which prevents SQL injection by escaping special characters in the user-supplied input from the URL parameter .
Once the data is retrieved from the database using the SQL query, the PHP script iterates over the result set using 'fetch_assoc()' to format it into an HTML table. It then echoes each row of data within HTML table tags, encoding special characters with 'htmlspecialchars()' to safely display it on the web page .
Currently, the script does not handle non-200 status codes, meaning failures do not provide feedback in the UI. To improve this, the 'onreadystatechange' function should check for non-200 status codes and update the page with an error message to inform the user of issues with the server request .