Click here to Skip to main content
14,699,570 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am new PHP learner and I wanted to call arrays order by id.But on the top of the page it shows
Notice: Undefined index: id in C:\xampp\htdocs\personal\post.php on line 9


What I have tried:

<?php

include "libs/db.php";
include "libs/config.php";
include "function.php";
$db = new database();
$id = $_GET['id'];
$query = "SELECT * from posts WHERE id = '$id'";
$posts = $db->select($query);

?>
<!DOCTYPE html>
<html>
<title>W3.CSS Template</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">
    <link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<style>
body,h1,h2,h3,h4,h5 {font-family: "Raleway", sans-serif}
</style>
<body class="w3-light-grey">

<!-- w3-content defines a container for fixed size centered content, 
and is wrapped around the whole page content, except for the footer in this example -->
<div class="w3-content" style="max-width:1400px">

<!-- Header -->
<header class="w3-container w3-center w3-padding-32"> 
  <a href="index.php?container.php"><h1>MY BLOG</h1></a>
  <p>Welcome to the blog of <span class="w3-tag">Programmer</span></p>
</header>
 <?php $row = $posts->fetch_array()  ;?>
<!-- Grid -->
<div class="w3-row">

<!-- Blog entries -->
   
<div class="w3-col l8 s12">
  <!-- Blog entry -->

  <div class="w3-card-4 w3-margin w3-white">
    <img src="images/<?php echo $row['image'];?>" alt="Nature" style="width:100%">
    <div class="w3-container">
    	
      <h3><?php echo $row['title'];?></h3>
      <h5>Posted by Sulaymonov on <span class="w3-opacity"><?php echo formatDate($row['date']);?></span></h5>
    </div>
    <div class="w3-container">
      <p><?php echo $row ['content'];?></p>
      <div class="w3-row">
        <div class="w3-col m8 s12">
         
        </div>

        <div class="w3-col m4 w3-hide-small">
          <p><span class="w3-padding-large w3-right">Comments   <span class="w3-tag">0</span></span></p>
        </div>
      </div>
    </div>
  </div>
  <hr>

<!-- END BLOG ENTRIES -->
</div>
Posted
Updated 2-Dec-17 7:02am

1 solution

The culprit is this
$id = $_GET['id'];
and it happens when $_GET['id'] is null. Always validate user's input, for example:
$id = isset($_GET['id']) ? $_GET['id'] : '';
   
Comments
ALEX8998 2-Dec-17 12:29pm
   
Thanks brother! the problem with id has been fixed! But it still keeps a Fatal error: Call to a member function fetch_array() on a non-object in C:\xampp\htdocs\personal\post.php

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900