How To Get Clicked Link Href Variable From One Page To Another Php HTML Mysql Xampp

How To View Clicked Id On A New Page In Php

This code retrieves/pulls data from mysql table and displays it on a page. When a user clicks view button next to each row on a webpage, then that rows id is displayed on a new page.

I Have Shared The Code For Index Page And Person Details. When You Click View A Person Link The Id Is Displayed On A New Page(User Details Page). Am Attaching The Id To The Href Part Of The Link. So When The Link Is Clicked It Pulls Id From Mysql Database And Opens It On A New Page. I Have Used Get Method To Extract Id From The Url Of A New Page. Then The Extracted Id Is Displayed. You Can Use This This Id To Make More Queries From Mysql Database Using Select Statements/Queries.

SOURCE CODE

index.php

<html>
<body>

<?php

//Including connect to database php code.
include 'connecttodatabase.php';
//SQL query for selecting all data from the database.
$result = mysqli_query($con,"SELECT * FROM person");
//Displaying table header on the browser.
echo "<table border='1'>
<tr>
<th>id</th>
<th>username</th>
<th>firstname</th>
<th>lastname</th>
</tr>";
//Displaying table rows on the browser.
while($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td> <a href='userdetails.php?id=". $row['id'] ."'>" .$row['id'] . "</a></td>";
  echo "<td>" . $row['username'] . "</td>";
  echo "<td>" . $row['firstname'] . "</td>";
  echo "<td>" . $row['lastname'] . "</td>";
  echo "<td> <a href='userdetails.php?id=". $row['id'] ."'> VIEW </a></td>";
  echo "</tr>";
}

echo "</table>";
//Closing Database Connection.
mysqli_close($con);
?>

</body>
</html>

userdetails.php

This is where user details are displayed when you select view button from index page.

<?php
//Check If Id Isset.
if ( isset( $_GET[ "id" ] ) )
   //Display Id On A Webpage.
   echo "<p>ID: " . $_GET[ "id" ] . "</p>";
?>

connecttodatabase.php

To display data from mysql database using php first you need to be connected

READ MORE  How To Upload Image In Mysql Database And Display It On A Webpage Using Php Html

The code below helps in connecting to the database, and storing connection in a variable which is used to make all database queries.

<?php 
 
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "personsdetails";

// Create connection
$con = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}  
 
 
 
?>

personsdetails.sql

-- phpMyAdmin SQL Dump
-- version 5.1.0
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jun 23, 2021 at 10:06 AM
-- Server version: 10.4.19-MariaDB
-- PHP Version: 8.0.6

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: `personsdetails`
--
CREATE DATABASE IF NOT EXISTS `personsdetails` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE `personsdetails`;

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

--
-- Table structure for table `person`
--

CREATE TABLE `person` (
  `id` int(11) NOT NULL,
  `username` varchar(255) NOT NULL,
  `firstname` varchar(255) NOT NULL,
  `lastname` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

--
-- Dumping data for table `person`
--

INSERT INTO `person` (`id`, `username`, `firstname`, `lastname`) VALUES(1, 'andy', 'Andrew', 'James');
INSERT INTO `person` (`id`, `username`, `firstname`, `lastname`) VALUES(2, 'John', 'Johnathan', 'Michael');

--
-- Indexes for dumped tables
--

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

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `person`
--
ALTER TABLE `person`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
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 */;

Posted in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *