简体   繁体   中英

creating a page that displays ID info on a template

Updated with suggestion by others but still seem to be stuck.

I'm using this php code here to display info from my database using the ID. I created a link on my main page that looks like this.

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

I have so when they click on the title of the article that it takes them to my php fiel which I named fetch.php and the code below is what is in there. I have built this around someone else's work. For some reason I can't get passed the first "else" statement. so I keep getting "you must select a valid location" I'm fairly new to php so I don't really understand why the code is failing.

<?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 "No match in database found."; // if no match is found, display this error 
} 
} 
}

Any help is appreciated. If you are able to find a better solution for me that would be great.

You shouldnt use $HTTP_GET_VARS its deprecated and unless its turned on it wont be populated. use $_GET instead.

if (isset($_GET['id']) == false)

Use $_GET for your if statement:

if (isset($_GET['id']) ==  false) 

Also, you need to convert your $_GET value to an integer, because it is currently a string.

Right after that if statement above, in the else , put this:

 $id = (int) $_GET['id']; 

That way your is_numeric() will work properly.

Try this;

<?php
require_once('Connections/XXXXXX.php');

if (isset($_GET['id'])) // check if id has been set
 {
$id = $_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 locationid = 'news.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 "No match in database found."; // if no match is found, display this error
}
}
}
else{
echo "You must select a location"; // if not, display this error
exit;
}
?>

Also, I need a clarification about news.post_id , from where are you grabbing this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM