Jump to content

Recommended Posts

Hi all.

 

I have a bit of a problem and can't figure out where I have gone wrong. I have 2 pages. 1 page displays an articles headline as a hyperlink and when selected takes the user to another page which displays all of the information for the selected article. The url of the article shows the articles id number, so if article 1 is selected for example, the url would be /article.php?id=1 for example.

 

The first page works fine. Displays the article headlines in order with the headline acting as a hyperlink. The code is below;

 

<?php
//include config file

//call query 
$query = "SELECT Headline, Preview, Body, ID FROM tbl ORDER BY ID DESC LIMIT 0,11";

//call result
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// see if any rows were returned 
if (mysql_num_rows($result) > 0) { 
    // yes 
    // print them one after another
     
    echo "<table cellpadding=10 border=1>";
    while($row = mysql_fetch_assoc($result)) { 
        echo "<tr>";
        echo "<td><a href='article.php?id=".$row['ID']."'>".$row['Headline']."</a></td>";
        echo "<td>".$row['Preview']."</td>";
        echo "</tr>";
    }
    echo "</table>";
} 
else { 
    // no 
    // print status message 
    echo "No rows found!"; 
} 

// free result set memory 
mysql_free_result($result); 

// close connection 
mysql_close($connection); 

?>

The second page doesn't work. It displays the error message that no article id was supplied, even though the URL has the article ID in it. The code is here;

 

<?php 
if(empty($_GET['ID'])) 
{ 
   echo "<p class='error'>No article ID supplied.</p>\n"; 
} 
else if(!$_POST['ID'])
{ 

   //include config file 
   
   
  
  $title = ($_POST['Headline']);
$query = "SELECT Headline, Body FROM tbl WHERE ID=".$_GET['ID']; 
   $result = mysql_query($query) or die("Query failed: ".mysql_error()); 
   
   if (mysql_num_rows($result) > 0) { 
    // yes 
    // print them one after another
     
    echo "<table cellpadding=10 border=1>";
    while($row = mysql_fetch_assoc($result)) { 
        echo "<tr>";
        echo "<td>".$row['Headline']."</a></td>";
        echo "<td>".$row['Body']."</td>";
        echo "</tr>";
    }
    echo "</table>";
} 
else { 
    // no 
    // print status message 
    echo "No rows found!"; 
}
// free result set memory 
mysql_free_result($result); 

// close connection 
mysql_close($connection); 
}
?>

I can't figure out why it's not displaying the data. I've triple checked it. It's been a long day so I have a feeling it's going to be a tiny mistake. Can anyone spot where I have gone wrong?

 

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/146391-solved-not-displaying-data/
Share on other sites

Change:

<?php 
if(empty($_GET['ID'])) 

to:

<?php 
print_r($_GET);
if(empty($_GET['ID']))

 

And double check to make sure that get index is correct. Just as a reminder, ID is not the same as id. It is case sensitive ;)

 

Thanks for your help, it helped me figure it out. I didn't need the print line, but on the testindex page it was the lower case id that caused the issue. I had a feeling it would be something simple! Cheers!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.