How to Add Data to the Database Using PHP in WordPress and Display it on a Page

July 27, 2023

How to Add Data to the Database Using PHP and Display it on a Page

Introduction: In this tutorial, we will learn how to create a form to insert data into a MySQL database using PHP and display the inserted data on a separate page. We will also create a search and sortable table to show all user reviews.

Some Points To Understating

1. Create a template file by WordPress Code functionality using PHP programming and php file is here insertdata.php file.
2. Create a blank page and use Insert Template created by the insertdata.php file.
3. Create table “reviews” in your MySQL Database and MySql Table is here reviews.sql file.
4. use this PHP code code4showallusersdata.php file and the file is here code4showallusersdata.php then this PHP code convert to Shortcode ( by XYZ PHP Code Plugin ).
5. Create Reviews Show Page without using any template and use the shortcode of code4showallusersdata.php file.
6. use this PHP code code4showuserdataindividual.php file and the file is here code4showuserdataindividual.php then this PHP code converts to a shortcode (by XYZ PHP Code Plugin).
7. Create a User Details Page without using any template and use the shortcode of code4showuserdataindividual.php file.
8. And Now you can style as per your requirement and you can use table code also with javascript for search and filter options.
9. Then You will do your user id convert to linkable data for open individual user data.

Explanation:-

Section 1: Creating the Insert Data Form

To start, we need to create a form to insert data into the database. This form will be created using PHP and will be placed in a file named “insertData.php”. Here’s the code for the “insertdata.php” file:

<?php
/**
 *
 * Template Name: Insert
 * Template Post Type: post, page
 *
 * 
 *
 */
?>
<?php
get_header();
?>
<!-- this file for insert data using custom php after create database table as per your requirement fields -->
<form method="POST">
    Review Id: <input type="text" name="review_id">
    <br><br>
    User Id: <input type="text" name="user_id">
    <br><br>
    First Name: <input type="text" name="first_name">
    <br><br>
    Last Name: <input type="text" name="last_name">
    <br><br>
    Mobile Number: <input type="text" name="mobile_number">
    <br><br>
    Email Id: <input type="text" name="email_id">
    <br><br>
    Check In Date: <input type="datetime-local" name="check_in_date">
    <br><br>
    Check Out Date: <input type="datetime-local" name="check_out_date">
    <br><br>
    Hear About: <input type="text" name="hear_about">
    <br><br>
    Reservation Type: <input type="text" name="reservation_type">
    <br><br>
    Purpose Of Visit: <input type="text" name="purpose_visit">
    <br><br>
    Service Rating: <input type="text" name="service_rating">
    <br><br>
    Clean Rating: <input type="text" name="clean_rating">
    <br><br>
    Food Rating: <input type="text" name="food_rating">
    <br><br>
    Staff Rating: <input type="text" name="staff_rating">
    <br><br>
    Overall Rating: <input type="text" name="overall_rating">
    <br><br>
    Suggestion: <input type="text" name="suggestion">
    <br><br>
    Reviewed Date - Time: <input type="datetime-local" name="reviews_dat_time">
    <br><br>
    Client Status: <input type="text" name="client_status">
    <br><br>

    <input type="submit" value="Insert Record" name="insert_btn">
    <br><br>
</form>

<?php 
if (isset($_POST['insert_btn'])) {
    $a = $_POST['review_id'];
    $b = $_POST['user_id'];
    $c = $_POST['first_name'];
    $d = $_POST['last_name'];
    $e = $_POST['mobile_number'];
    $f = $_POST['email_id'];
    $g = $_POST['check_in_date'];
    $h = $_POST['check_out_date'];
    $i = $_POST['hear_about'];
    $j = $_POST['reservation_type'];
    $k = $_POST['purpose_visit'];
    $l = $_POST['service_rating'];
    $m = $_POST['clean_rating'];
    $n = $_POST['food_rating'];
    $o = $_POST['staff_rating'];
    $p = $_POST['overall_rating'];
    $q = $_POST['suggestion'];
    $r = $_POST['reviews_dat_time'];
    $s = $_POST['client_status'];

    global $wpdb;
//     $table_name = $wpdb->prefix . 'reviews';
    $data = array(
        'review_id' => $a,
        'user_id' => $b,
        'f_name' => $c,
        'l_name' => $d,
        'mobile_number' => $e,
        'email' => $f,
        'check_in_dt' => $g,
        'check_out_dt' => $h,
        'hear_about' => $i,
        'reservation_type' => $j,
        'purpose_of_visit' => $k,
        'service_rating' => $l,
        'clean_rating' => $m,
        'food_rating' => $n,
        'staff_rating' => $o,
        'overall_rating' => $p,
        'suggestion' => $q,
        'reviewed_dt_tm' => $r,
        'client_status' => $s
    );

    $wpdb->insert("reviews", $data);

    if ($wpdb->insert_id) {
        echo "<script>alert('Data inserted')</script>";
    } else {
        echo "<script>alert('Data not inserted')</script>";
    }
}

get_footer();
?>

Section 2: Creating the Reviews Table

Next, we need to create a table named “reviews” in the MySQL database. We can do this by executing the SQL query provided in the “reviews.sql” file. Use a tool like phpMyAdmin or MySQL command-line to execute this query.

-- phpMyAdmin SQL Dump
-- version 5.2.1
-- https://www.phpmyadmin.net/
--
-- Host: localhost
-- Generation Time: Jul 23, 2023 at 07:38 AM
-- Server version: 5.7.23-23
-- PHP Version: 8.1.21

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `u188470928_JoLzA`
--

-- --------------------------------------------------------

--
-- Table structure for table `reviews`
--

CREATE TABLE `reviews` (
  `id` int(11) NOT NULL,
  `review_id` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `user_id` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `f_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  `l_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  `mobile_number` bigint(12) NOT NULL,
  `email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `check_in_dt` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
  `check_out_dt` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
  `hear_about` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  `reservation_type` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  `purpose_of_visit` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  `service_rating` varchar(11) COLLATE utf8_unicode_ci NOT NULL,
  `clean_rating` varchar(11) COLLATE utf8_unicode_ci NOT NULL,
  `food_rating` varchar(11) COLLATE utf8_unicode_ci NOT NULL,
  `staff_rating` varchar(11) COLLATE utf8_unicode_ci NOT NULL,
  `overall_rating` float NOT NULL,
  `suggestion` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
  `reviewed_dt_tm` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
  `client_status` int(2) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `reviews`
--

INSERT INTO `reviews` (`id`, `review_id`, `user_id`, `f_name`, `l_name`, `mobile_number`, `email`, `check_in_dt`, `check_out_dt`, `hear_about`, `reservation_type`, `purpose_of_visit`, `service_rating`, `clean_rating`, `food_rating`, `staff_rating`, `overall_rating`, `suggestion`, `reviewed_dt_tm`, `client_status`) VALUES
(1, '5435354545', 'sdfdsdsdsfsdfsds', 'Mohit', 'Tomar', 8787878787887, 'dgfgfdgdf@gmail.com', '2023-05-16 14:42:48', '2023-05-17 14:42:48', 'facebook', 'fdgfhgf', 'gfggfhgfhfg', '4.9', '4.8', '4.5', '4.5', 4.7, 'gfdgfdg gdfgdfgdfgdf dfghfdgdfgfdgdfg', '2023-05-16 14:42:48', 0),
(2, '1122334455', '110088', 'Tapan', 'Sharma', 9876543210, 'tapan@gmail.com', '2023-05-03 18:22:13', '2023-05-31 18:22:13', 'Google', 'Cards', 'Family Visit', '5', '5', '5', '5', 5, 'Please Increase your price overall everything is awesome', '2023-05-12 18:22:13', 1),
(3, '108', '108', 'Rohit ', 'Thakur', 987654321, 'rohitt@gmail.com', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Google', 'General', 'Family visit', '5', '5', '5', '5', 5, 'overall nice', '0000-00-00 00:00:00', 0),
(4, '108', '108', 'Rohit ', 'Thakur', 987654321, 'rohitt@gmail.com', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Google', 'General', 'Family visit', '5', '5', '5', '5', 5, 'overall nice', '0000-00-00 00:00:00', 0),
(5, '108', '108', 'Rohit ', 'Thakur', 987654321, 'rohitt@gmail.com', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Google', 'General', 'Family visit', '5', '5', '5', '5', 5, 'overall nice', '0000-00-00 00:00:00', 0),
(6, '', '', 'abc', 'def', 9098877656, 'abc@gmail.com', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'social', 'other', 'other', 'ServiceRati', 'Clean 0', 'food 0', 'Staff 0', 2.3, 'Good', '0000-00-00 00:00:00', 0),
(7, '', '', 'abcd', 'def', 9098877656, 'abc@gmail.com', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'social', 'other', 'other', '0', '0', '0', '0', 2.3, 'Good', '0000-00-00 00:00:00', 0),
(8, '', '', 'abcd', 'def', 9098877656, 'abc@gmail.com', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'social', 'other', 'other', 'ServiceRati', '0', '0', '0', 2.3, 'Good', '0000-00-00 00:00:00', 0),
(9, '', '', 'abcd', 'def', 9098877656, 'abc@gmail.com', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'social', 'other', 'other', 'ServiceRati', '0', '0', '0', 2.3, 'Good', '0000-00-00 00:00:00', 0),
(10, '', '', 'kkjk', 'jhjh', 1111111111, 'dfsdfd@mail.com', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Ads', 'Online', 'Wedding', '3', '3', '3', '3', 4, '', '0000-00-00 00:00:00', 0),
(11, '', '', 'fgfg', 'dfdfd', 1112122123, 'ddfg@gmail.com', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Ads', 'Other', 'Business', '0', '3', '4', '4', 3, 'good', '0000-00-00 00:00:00', 0),
(12, '', '', 'dfdfd', 'sdfsdf', 900999, 'fgf@gmail.com', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Ads', 'Travel Agency', 'Vacation', '0', '1', '2', '3', 2.5, '', '0000-00-00 00:00:00', 0),
(13, '', '', 'dfdfd', 'sdfsdf', 900999, 'fgf@gmail.com', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Ads', 'Travel Agency', 'Vacation', '0', '1', '2', '3', 2.5, '', '0000-00-00 00:00:00', 0),
(14, '', '', 'dfdfd', 'sdfsdf', 900999, 'fgf@gmail.com', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Ads', 'Travel Agency', 'Vacation', '0', '1', '2', '3', 2.5, '', '0000-00-00 00:00:00', 0),
(15, '', '', 'dfdfd', 'sdfsdf', 900999, 'fgf@gmail.com', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Ads', 'Travel Agency', 'Vacation', '0', '1', '2', '3', 2.5, '', '0000-00-00 00:00:00', 0),
(16, '', '', 'dfdrdgrg', 'rrgfdgg', 1111112222, 'ffgg@gmail.com', '05-18-2023', '05-20-2023', 'Friends and family', 'Online', 'Wedding', '0', '0', '0', '0', 1, '', '0000-00-00 00:00:00', 0),
(17, '', '', 'dfdsf', 'sfsdf', 1111222323, 'vfdg@gmail.com', '05-18-2023', '05-22-2023', 'Social media', 'Online', 'Business', '0', '0', '0', '0', 1.5, '', '0000-00-00 00:00:00', 0),
(18, '', '', 'qqq', 'wwww', 1212212121, 'sdsdf@gmail.com', '05-18-2023', '05-21-2023', 'Friends and family', 'Travel Agency', 'Vacation', '2', '3', '4', '3', 3.5, '', '0000-00-00 00:00:00', 0),
(19, '', '', 'sdsdf', 'sdfsdf', 1111121212, 'dfdf@gmail.com', '05-18-2023', '05-22-2023', 'Social media', 'Online', 'Wedding', '2', '3', '4', '3', 2.5, '', '0000-00-00 00:00:00', 0),
(20, '', '', 'tapan', 'kumar', 1212132334, 'eret@gmail.com', '05-20-2023', '05-24-2023', 'Social media', 'Online', 'Wedding', '2', '2', '2', '2', 3.5, '', '', 0),
(21, '', '', 'mohitt ', 'Kumar ', 1115888595, 'mohitt084@gmail.com', '05-18-2023', '05-19-2023', 'Social media', 'Online', 'Vacation', '1', '1', '1', '1', 1, 'good ', '', 0),
(22, '', '', 'yghh', 'hhj', 6665555555, 'ggg@gmail.com', '05-18-2023', '05-19-2023', 'Social media', 'Online', 'Wedding', '0', '1', '2', '3', 5, 'abc', '', 0),
(23, '', '', 'fhhfezgj', 'sawhjsa', 4828669814, 'sghjha@gmail.com', '05-18-2023', '05-27-2023', 'Social media', 'Online', 'Vacation', '2', '2', '1', '2', 4.5, 'efhcaagb', '', 0),
(24, '', '', 'ffghfg', 'sdfsdf', 1122132432, 'fgfghfgh@gmail.com', '05-16-2023', '05-17-2023', 'Friends and family', 'Online', 'Business', '4', '4', '4', '4', 5, 'good', '05-19-2023', 0),
(25, '', '', 'Mohitt', 'kumar', 1212312323, 'fbfg@gmail.com', '05-16-2023', '05-18-2023', 'Social media', 'Online', 'Business', '4', '4', '4', '4', 4.5, 'good', '05-19-2023', 0),
(26, '', '', 'Mohit', 'Singh', 9909090900, 'jjhhhjm@gmail.com', '05-15-2023', '05-18-2023', 'Ads', 'Online', 'Vacation', '4', '4', '3', '4', 4.5, 'good', '05-19-2023', 0),
(27, '', '0', 'Sachin', 'Thakur', 987654321, 'sachin@hotmail.com', '2023-05-27T18:39', '2023-05-25T18:39', 'Google', 'General', 'Family visit', '5', '5', '5', '5', 5, 'overall nice', '2023-05-27T18:39', 1),
(28, '', '', 'Ms.', 'Dhoni', 8880546787, 'dhoni@gmail.com', '05-07-2023', '05-18-2023', 'Friends and family', 'Online', 'Vacation', '4', '4', '4', '4', 5, 'Dhoni is the best player.', '05-20-2023', 0),
(29, '', '', 'TAPAN', 'padhi', 8980908989, 'jjhjhj@gmail.com', '05-15-2023', '05-18-2023', 'Social media', 'Online', 'Wedding', '2', '2', '3', '4', 5, 'good services', '05-20-2023', 0),
(30, '', '', 'fsfssfsf', 'rer', 1121212132, 'dgfdfg@gmail.com', '05-22-2023', '05-23-2023', 'Social media', 'Online', 'Wedding', '4', '3', '0', '2', 4, 'Good', '05-22-2023', 0),
(31, '', '', 'ii', 'ere', 8787876767, 'jj@gmail.com', '05-23-2023', '05-27-2023', 'Social media', 'Online', 'Business', '4', '4', '3', '3', 3.5, 'good services', '05-23-2023', 0),
(32, '', '', 'Mohitt', 'Sing', 1213131313, 'fgfgh@gmail.com', '11-05-2023', '22-05-2023', 'Social media', 'Travel Agency', 'Wedding', '4', '5', '4', '5', 5, 'good services', '23-05-2023', 0),
(33, '', '', 'Tapan', 'kumar', 8018514398, 'tapan@gmail.com', '17-05-2023', '22-05-2023', 'Ads', 'Online', 'Business', '5', '5', '4', '5', 5, 'Good services', '23-05-2023', 0);

--
-- Indexes for dumped tables
--

--
-- Indexes for table `reviews`
--
ALTER TABLE `reviews`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `reviews`
--
ALTER TABLE `reviews`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=34;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

 

 

 

Section 3: Displaying All User Reviews

Now, we will create a page to display all the user reviews in a searchable and sortable table. We will use a PHP file named “file4showuserdataindividual.php” and utilize the WordPress $wpdb class to fetch data from the “reviews” table. Additionally, we will use the DataTables library to make the table sortable. Below is the code for the “file4showuserdataindividual.php” file:

<?php
global $wpdb;

// this is code4showallusersdata file

$sql = "SELECT * FROM reviews";
$result = $wpdb->get_results($sql);

if ($result) {
    echo "<style>";
    echo "body { font-family: Arial; }";
    echo ".table_container { padding: 10px 12px 0px 12px; border: 1px solid #ccc; }";
    echo ".table_container th { background-color: #32CD32; color: white; font-weight: bold; border-left: 1px solid white; cursor: pointer; padding: 8px; }";
    echo ".table_container th:hover { background-color: cyan; color: black; }";
    echo ".table_container td, .table_container th { padding: 8px; text-align: left; }";
    echo ".table_container tr:nth-child(even) { background-color: #f2f2f2; }";
    echo ".table_container input { margin-bottom: 10px; }";
    echo "</style>";
    echo "</head>";
    echo "<body>";
    
    echo "<div class=\"table_container\">";
    echo "<input type=\"text\" id=\"myInput\" placeholder=\"Search for names..\">";
    echo "<table id=\"myTable\">";
    echo "<thead><tr><th style=\"width: 70px;\" data-sort=\"numeric\">S.No.</th><th data-sort=\"alpha\">User Id</th><th data-sort=\"alpha\">First Name</th><th data-sort=\"alpha\">Last Name</th><th data-sort=\"numeric\">Mobile Number</th><th data-sort=\"alpha\">Email Id</th><th data-sort=\"date\">Check In Date</th><th data-sort=\"date\">Check Out Date</th><th data-sort=\"alpha\">Hear About</th><th data-sort=\"alpha\">Reservation Type</th><th data-sort=\"alpha\">Purpose Of Visit</th><th data-sort=\"numeric\">Service Rating</th><th data-sort=\"numeric\">Clean Rating</th><th data-sort=\"numeric\">Food Rating</th><th data-sort=\"numeric\">Staff Rating</th><th data-sort=\"numeric\">Overall Rating</th><th data-sort=\"alpha\">Suggestion</th><th data-sort=\"date\">Reviewed Date & Time</th><th data-sort=\"alpha\">Client Status</th></tr></thead>";
    
    echo "<tbody>";
    foreach ($result as $row) {
        $user_id = sprintf("%07d", $row->id); // Generate a 7-digit user ID based on the 'id' field
        $user_page_url = 'https://hoteladmin.softreey.com/user-details/?user_id=' . str_pad($user_id, 7, '0', STR_PAD_LEFT);
        // Generate the dynamic page URL
        
        echo "<tr>";
        echo "<td>" . $row->id . "</td>";
        echo "<td><a href='" . $user_page_url . "'>" . $user_id . "</a></td>";
        echo "<td>" . $row->f_name . "</td>";
        echo "<td>" . $row->l_name . "</td>";
        echo "<td>" . $row->mobile_number . "</td>";
        echo "<td>" . $row->email . "</td>";
        echo "<td>" . $row->check_in_dt . "</td>";
        echo "<td>" . $row->check_out_dt . "</td>";
        echo "<td>" . $row->hear_about . "</td>";
        echo "<td>" . $row->reservation_type . "</td>";
        echo "<td>" . $row->purpose_of_visit . "</td>";
        echo "<td>" . $row->service_rating . "</td>";
        echo "<td>" . $row->clean_rating . "</td>";
        echo "<td>" . $row->food_rating . "</td>";
        echo "<td>" . $row->staff_rating . "</td>";
        echo "<td>" . $row->overall_rating . "</td>";
        echo "<td>" . $row->suggestion . "</td>";
        echo "<td>" . $row->reviewed_dt_tm . "</td>";
        echo "<td>" . $row->client_status . "</td>";
        echo "</tr>";
    }
    echo "</tbody>";
    
    echo "</table>";
    echo "</div>";

    echo "<script>";
    echo "document.addEventListener('DOMContentLoaded', function() {";
    echo "    var input = document.getElementById('myInput');";
    echo "    input.addEventListener('keyup', function() {";
    echo "        var filter, table, tr, td, i, txtValue;";
    echo "        filter = input.value.toUpperCase();";
    echo "        table = document.getElementById('myTable');";
    echo "        tr = table.getElementsByTagName('tr');";
    echo "        for (i = 0; i < tr.length; i++) {";
    echo "            td = tr[i].getElementsByTagName('td')[2];";
    echo "            if (td) {";
    echo "                txtValue = td.textContent || td.innerText;";
    echo "                if (txtValue.toUpperCase().indexOf(filter) > -1) {";
    echo "                    tr[i].style.display = '';";
    echo "                } else {";
    echo "                    tr[i].style.display = 'none';";
    echo "                }";
    echo "            }";
    echo "        }";
    echo "    });";
    echo "";
    echo "    var headers = document.getElementsByTagName('th');";
    echo "    for (var i = 0; i < headers.length; i++) {";
    echo "        headers[i].addEventListener('click', function() {";
    echo "            var table = document.getElementById('myTable');";
    echo "            var rows = table.rows;";
    echo "            var switching = true;";
    echo "            var shouldSwitch = false;";
    echo "            var sortDirection = 'asc';";
    echo "            var columnIndex = this.cellIndex;";
    echo "            var dataType = this.getAttribute('data-sort');";
    echo "";
    echo "            while (switching) {";
    echo "                switching = false;";
    echo "                for (var i = 1; i < (rows.length - 1); i++) {";
    echo "                    var currentRow = rows[i];";
    echo "                    var nextRow = rows[i + 1];";
    echo "                    var shouldSwitch = false;";
    echo "                    var currentValue = currentRow.getElementsByTagName('td')[columnIndex].innerHTML;";
    echo "                    var nextValue = nextRow.getElementsByTagName('td')[columnIndex].innerHTML;";
    echo "";
    echo "                    if (dataType === 'alpha') {";
    echo "                        if (sortDirection === 'asc') {";
    echo "                            if (currentValue.toLowerCase() > nextValue.toLowerCase()) {";
    echo "                                shouldSwitch = true;";
    echo "                                break;";
    echo "                            }";
    echo "                        } else if (sortDirection === 'desc') {";
    echo "                            if (currentValue.toLowerCase() < nextValue.toLowerCase()) {";
    echo "                                shouldSwitch = true;";
    echo "                                break;";
    echo "                            }";
    echo "                        }";
    echo "                    } else if (dataType === 'numeric') {";
    echo "                        if (sortDirection === 'asc') {";
    echo "                            if (Number(currentValue) > Number(nextValue)) {";
    echo "                                shouldSwitch = true;";
    echo "                                break;";
    echo "                            }";
    echo "                        } else if (sortDirection === 'desc') {";
    echo "                            if (Number(currentValue) < Number(nextValue)) {";
    echo "                                shouldSwitch = true;";
    echo "                                break;";
    echo "                            }";
    echo "                        }";
    echo "                    } else if (dataType === 'date') {";
    echo "                        if (sortDirection === 'asc') {";
    echo "                            if (Date.parse(currentValue) > Date.parse(nextValue)) {";
    echo "                                shouldSwitch = true;";
    echo "                                break;";
    echo "                            }";
    echo "                        } else if (sortDirection === 'desc') {";
    echo "                            if (Date.parse(currentValue) < Date.parse(nextValue)) {";
    echo "                                shouldSwitch = true;";
    echo "                                break;";
    echo "                            }";
    echo "                        }";
    echo "                    }";
    echo "                }";
    echo "";
    echo "                if (shouldSwitch) {";
    echo "                    currentRow.parentNode.insertBefore(nextRow, currentRow);";
    echo "                    switching = true;";
    echo "                } else {";
    echo "                    if (sortDirection === 'asc') {";
    echo "                        sortDirection = 'desc';";
    echo "                    } else {";
    echo "                        sortDirection = 'asc';";
    echo "                    }";
    echo "                }";
    echo "            }";
    echo "        });";
    echo "    }";
    echo "});";
    echo "</script>";

} else {
    echo "0 results";
}
?>
<!-- script for table set up -->
<script>
jQuery(document).ready(function($) {
    $('#myTable').DataTable();
  });
</script>

</html>

 

Section 4: Creating Individual User Review Page

Next, we will create a page to display individual user reviews based on their unique user ID. We will use a PHP file named “file4showuserdataindividual.php” and pass the user ID as a parameter in the URL. Below is the code for the “file4showuserdataindividual.php” file:

<?php
$user_id = $_GET['user_id'];
// this file is file4showuserdataindividual file
// Query the user data based on the user ID

global $wpdb;
$sql = "SELECT * FROM reviews WHERE id = %d";
$result = $wpdb->get_row($wpdb->prepare($sql, $user_id));

if ($result) {
    echo "<h1>User Details</h1>";
    echo "<p>User ID: " . $result->id . "</p>";
    echo "<p>First Name: " . $result->f_name . "</p>";
    echo "<p>Last Name: " . $result->l_name . "</p>";
    echo "<p>Mobile Number: " . $result->mobile_number . "</p>";
    echo "<p>Check In Date: " . $result->check_in_dt . "</p>";
    echo "<p>Check Out Date: " . $result->check_out_dt . "</p>";
    echo "<p>Hear About Our Hotel: " . $result->hear_about . "</p>";
    echo "<p>Reservation Type: " . $result->reservation_type . "</p>";
    echo "<p>Purpose Of Visit: " . $result->purpose_of_visit . "</p>";
    echo "<p>Service Rating: " . $result->service_rating . "</p>";
    echo "<p>Cleaning Rating: " . $result->clean_rating . "</p>";
    echo "<p>Food Rating: " . $result->food_rating . "</p>";
    echo "<p>Staff Rating: " . $result->staff_rating . "</p>";
    echo "<p>Overall Rating: " . $result->overall_rating . "</p>";
    echo "<p>Suggestion Message: " . $result->suggestion . "</p>";
    echo "<p>Review Giving Date: " . $result->reviewed_dt_tm . "</p>";
    echo "<p>Client Status: " . $result->client_status . "</p>";
    
    // Add more user details as needed
} else {
    echo "<p>User not found.</p>";
}
?>

Conclusion: In this tutorial, we have learned how to add data to a MySQL database using PHP and display the inserted data on a separate page. We have also created a searchable and sortable table to display all user reviews. By following these steps, you can create an attractive blog post demonstrating the process of adding data to a database and presenting it in a user-friendly manner.