1

let say I have a post that can be found on page post.php?pid=60 but if the user changes the url to post.php?pid=95 the page is displayed all weird is there a way I can have them redirected to another page that says post dosn't exist or something? If so how would I do this? And is this the best way to handle none existent pages? If not what is preferred?

I'm using PHP & MySQL if that helps.

1
  • 2
    You need to add the logic to the post.php, after it fetches info from the database (because you don't know valid IDs otherwise), and redirect based on no rows being returned in the resultset. Commented Aug 30, 2010 at 2:53

3 Answers 3

2

get the post from DB by the given $_GET["pid"]

if a post is found (ie. given pid existed)

then display it normally

else include the error page content / use header to redirect to the error page, etc.

Hope the logic helps you

Sign up to request clarification or add additional context in comments.

Comments

1

well, you could try to select the post from the database, and if the query brings nothing you redirect to the page that shows all posts. it makes more sense to me than exhibiting a "page not found" message (in this case). the idea is something like this:

$result = query('SELECT * FROM posts WHERE id=60');
if( ! $result)
    redir('all_posts.php');

this is just for picturing the problem. i'm assuming that the query function returns false, null or even an empty array if no rows are returned, instead of the usual mysql resource.

just remember to add an exit; (could be in the redirect function) to not let the page renders all weird.

Comments

0

You should be checking if there is data being returned in the database, if there is not then you display the error message.

Do you have any supporting code? Here is a mock-up:

$pid = isset($_GET['pid'])?(int)$_GET['pid']:0; // use the ternary operator to set a default value

if (!empty($pid) {
    $res = mysql_query("SELECT columns FROM table_name WHERE pid = $pid") or trigger_error('MySQL Returned: ' . mysql_error());
    $rows = mysql_num_rows($res);
    if ($rows > 0) {
         // display the data
    }else {
         // There is no data so display the no-data page
    }
}

It may not work exactly for you, as you will have to tweak it, but should give you a rough idea.

1 Comment

Yes I have code but I just wanted to know the basic idea of how to do this thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.