1. Database Table (employee)
The table definition remains the same. Here’s the SQL code for creating the table:
Make sure that the the userid
column is set to AUTO_INCREMENT
for automatic id generation.
2. database.php
Modify your database.php
to use PDO (PHP Data Objects) for a better and safer database connection, avoiding issues with mysqli
and providing easier handling for prepared statements.
3. insert.php
Your insert.php
file should contain the form where the user submits their data. Make sure you have proper HTML and form validation.
4. process.php
Here is how you can refactor process.php
to use prepared statements to securely insert data into the database:
5. retrieve.php
This file retrieves data from the employee
table and displays it in a table using Bootstrap and DataTables for better display.
Conclusion
-
Security: The code uses prepared statements for secure database queries to prevent SQL injection.
-
PDO: I've refactored your code to use PDO for database interaction instead of mysqli, which is more modern and flexible.
-
Data Retrieval: The
retrieve.php
file fetches all records from the database and displays them using DataTables, which allows for sorting and searching.