Displaying single article content on click of title name

Hello,

I have a situation here where I want to a new display page to show only content under a certain ID from sql database. I have something written now and it goes something like this.

I have on my main page a list of articles from many different categories that display in order by most recent. (This is like a blog, I suppose) On the title of the document which is saved in mysql database is under the name ‘title’ i have them echoed from there including the primary key which I named ‘post_id’. I also have it linked so that it is going to a php which will take the ID and use it to search the database for the post_id and then relay the information back on the page.

<h1><a href="fetch.php?id=<?php echo $row_getDisplay['post_id']; ?>"<?php echo $row_getDisplay['title']; ?></a></h1>

This is the link that takes us to the php file.

In my php file it looks like this.

<?php require_once('Connections/XXXXXX.php'); ?>
<?php
if (isset($_GET['id']) ==  false) // check if id has been set
{
echo "You must select a location"; // if not, display this error
exit;
} else {
$id = (int) $_GET['id'];
if (is_numeric($id) == false)
**{
echo "You must select a valid location.";
} else {**

mysql_select_db($database_XXXXXX, $XXXXXX);
$query = MYSQL_QUERY("SELECT * FROM news WHERE post_id ");

if (MYSQL_NUM_ROWS($query) == "1")

{
$fetch = MYSQL_FETCH_ARRAY($query); // set $fetch to have the values from the table
echo "Title: " . $fetch['title'] . "<BR>"; // output the info
echo "Blog: " . $fetch['blog_entry'] . "<BR>"; // etc...
echo "Author: " . $fetch['author'] . "<BR>"; // etc...
} else {
echo [B]"No match in database found."[/B]; // if no match is found, display this error
}
}
}
?>

What I have in bold is the message I get when I click the title of the link and it takes me to the fetch.php page and displays “No match in database found.” If anyone has a work around for this or maybe can re-write the code so that it can do what I want that would be greatly appreciated. As of now I’m stuck and don’t want to move on until I get it figured out. I might even be doing this the hard way as well. Thanks for the help.


<?php
if (! isset($_GET['id']) || (int) $_GET['id'] === 0 ) {
echo "Incorrect input, aborting"; 
exit;  
} 

// from here on id MUST been set and it IS an iteger > 0
// dont bother with the else ...

mysql_select_db($database_XXXXXX, $XXXXXX); 

$sql = "SELECT * FROM news WHERE post_id = " . $_GET['id']); 

// a line of debug to make sure things are as expected
echo $sql;

$query = MYSQL_QUERY($sql);

That first line checks if id is set, and then typecasts it to an int (even if it contained strings it is set to zero) if that does happen then fail early.

There are other problems with you script, but that should remove some of the sets of curly brackets and make things a bit clearer.

Good job on the casting to integers with (int) by the way :wink:

Thank you Cups. you got it to work exactly like how I wanted to. Now all I have to do is add a template to it. Thank you.

Off topic (but not much): you might want to read this page of the php manual: http://it.php.net/manual/en/intro.mysql.php