<?php
require_once('database.php');
$query="select * from news";
$result=mysqli_query($dbc,$query);
while($row=mysqli_fetch_array($result)){
$Title=$row['Title'];
//$Content=$row['Content'];
echo $Title;
echo "<a href=''>$Title</a><br>";
}

?>

Hi friends....
I have a table named news which contain columns such as Title, Content,NewsId and many more.
I want to display titles in form of link and when we click any link we get its corresponding Content.
By above code I display Title as link but don't know how to display its corresponding Contents on click.

Plzzzzzzzzzzzzzzzz
Help me anyone...................

Dani AI

Generated

A quick diagnosis and a simple, safe fix that follows up on and :

The "Undefined index: NewsId" notice is caused by reading the wrong superglobal. Links send data via the query string (GET), but the code in Post #2 reads $_POST['NewsId']. Also note a missing semicolon in 's snippet after $NewsId=$row['NewsId'] which would cause a parse error. Using $_REQUEST masks whether the value came from GET or POST and is not recommended for clarity or security.

A clear pattern: output links with a query parameter, then on the same page (or a separate page) check for that GET parameter, validate it, and fetch the single row with a prepared statement. Example (mysqli, procedural style):

<?php
require_once 'database.php'; // $dbc = mysqli_connect(...)

error_reporting(E_ALL);
ini_set('display_errors', 1);

if (isset($_GET['id'])) {
    $id = (int) $_GET['id'];
    $stmt = mysqli_prepare($dbc, 'SELECT Title, Content FROM news WHERE NewsId = ? LIMIT 1');
    mysqli_stmt_bind_param($stmt, 'i', $id);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_bind_result($stmt, $title, $content);
    if (mysqli_stmt_fetch($stmt)) {
        echo '<h1>' . htmlspecialchars($title, ENT_QUOTES) . '</h1>';
        echo '<div>' . nl2br(htmlspecialchars($content, ENT_QUOTES)) . '</div>';
    } else {
        echo 'Article not found.';
    }
    mysqli_stmt_close($stmt);
} else {
    $res = mysqli_query($dbc, 'SELECT NewsId, Title FROM news ORDER BY NewsId DESC');
    while ($r = mysqli_fetch_assoc($res)) {
        printf('<a href="?id=%d">%s</a><br>', (int)$r['NewsId'], htmlspecialchars($r['Title'], ENT_QUOTES));
    }
}
?>

Key notes

  • Use $_GET (or filter_input(INPUT_GET, ...)) for links; use isset() to avoid undefined index.
  • Prefer prepared statements to prevent SQL injection. Never interpolate raw user input into SQL.
  • Escape HTML output with htmlspecialchars; if content must allow HTML, sanitize server-side (or store trusted HTML).
  • Keep error_reporting on during development and check link hrefs with "view source" to confirm the query string.

This approach fixes the undefined-index error, avoids the parse error in 's example, and gives a safer production-ready pattern for listing titles and showing content on click.

Recommended Answers

All 3 Replies

<?php
require_once('database.php');
$query="select * from news";
$result=mysqli_query($dbc,$query);
while($row=mysqli_fetch_array($result)){
$NewsId=$row['NewsId']
$Title=$row['Title'];
//$Content=$row['Content'];
echo $Title;
echo "<a href=somePage.php?NewsId=$NewsId>$Title</a><br>";
}
?>

In the somePage.php use following

<?php
require_once('database.php');
$NewsId = $_POST['NewsId'];
$query="select * from news where NewsId = '$NewsId'";
$result=mysqli_query($dbc,$query);
while($row=mysqli_fetch_array($result)){
$Content=$row['Content'];
echo $Content;
}
?>

Or you can use that in the same page, then use the following
Add the condition above the news content code

<?php
if($_POST['NewsId'])
{
require_once('database.php');
$NewsId = $_POST['NewsId'];
$query="select * from news where NewsId = '$NewsId'";
$result=mysqli_query($dbc,$query);
while($row=mysqli_fetch_array($result)){
$Content=$row['Content'];
echo $Content;
}
}
?>

Hope this code work for you.
I wrote it directly here.

Thanks for ur code but this code gives an error "Notice: Undefined index: NewsId in C:\wamp\www\NGOProject\somePhp.php on line 3".
It means php not recognise $NewsId.
Help me PLzzzzzzzzzzzzzzz........
Any one???????????

Ok make it as $NewsId = $_REQUEST;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.