Using page id to load data from database

Hello. What I am trying to do is to load data from a database into different pages. So if a user clicks on a link (say link1) then the data from the first field (id = 1) is loaded, and so on for all the other pages. However, when I click the links the same page is displayed.

I have the following in a setup file:

# page setup 
if (isset($_GET['page'])) {
	
	$pageid = $_GET['page'];
	
} else {
	
	$pageid = 1;
}

$query = "SELECT * FROM contents WHERE id = $pageid";
$result = mysqli_query($link, $query);
$page = mysqli_fetch_assoc($result);

There are two pages: contents.php which contains the links, and the display.php where the data should display.

Here is part of the html code for contents.php:

<?php include('dbconnection/db-con.php'); ?>
<!DOCTYPE html>
<html lang="en">
	
<head>
		
	<?php include('templates/head.php'); ?>

</head>

<body>

	<?php include('templates/menu.php'); ?>

	<div class="fill">
			
		<h1>Contents</h1>
			
	</div><!-- container end: -->
	
	<div class="container">
		
		<div class="row">
		  	
			<div class="col-sm-4">
		    
		    	<h4>&#x2609; <a href="display.php?page=1">Prehistoric Egypt</a></h4>
			
					<ul>
						
						<li><a href="display.php?page=2">Lower Egypt</a></li>

Here is part of the html code for display.php:

    <?php 

    include('dbconnection/db-con.php'); 

    $query = "SELECT * FROM contents";
    $result = mysqli_query($link, $query);
    $row = mysqli_fetch_array($result);

    ?>
    <!DOCTYPE html>
    <html lang="en">
    	
    <head>
    		
    	<?php include('templates/head.php'); ?>
    	
    </head>

    <body>

	<?php include('templates/menu.php'); ?>
	

	<div class="fill">
					
		<h1>Ancient Egypt - <?php echo $row['sitetitle'];?></h1>
			
	</div><!-- container end: -->
	
	<div class="container">
		
		<?php echo $row['intro'];?>
	
	</div><!-- container end: -->
	
	<div class="container">
		
		<?php echo $row['slider'];?>
				
	</div>
	
	<div class="container">
		
		<?php echo $row['main'];?>
		
	</div>

Any help will be appreciated…

Your code is wide open to an SQL Injection Attack. You need to use Prepared Statements. I would also highly recommend you use PDO.

Here is a tutorial to get you going.
https://phpdelusions.net/pdo

And where exactly are you using that on the display.php page?

Sponsor our Newsletter | Privacy Policy | Terms of Service