Jump to content

Recommended Posts

Afternoon All.  I wish to re-direct users to a 404 error page on my site if an article does not exist in my database.  Here's my code:

 

 

$SQL = "SELECT  headline FROM news WHERE news_id=".mysql_real_escape_string($_GET['news_id']); $result = mysql_query($SQL) OR die(mysql_error()); $num = mysql_num_rows($result);//** Check that the entry exists otherwise send to error pageif ($num > 0) {  $row = mysql_fetch_array($result);  $headline = $row['headline'];} else {  echo "Why is this printed? - I should be leaving this page?";  header("Location: error.php");  exit;}

 

 

Now the wierd thing is that when I enter a news_id  for a value that does not exist it prints the message Why is this printed? - I should be leaving this page? so it's actually going to the ELSE statement which is good, but surely it should not do this as I ask the page to re-direct?

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/214912-redirect-users-to-a-new-page/
Share on other sites

Actually, sorry how would I reorder the structure of my PHP to do this.  I've misunderstood, do you mean re-order what i've done here?:

 

$SQL = "SELECT headline FROM news WHERE news_id=".mysql_real_escape_string($_GET['news_id']); 
$result = mysql_query($SQL) OR die(mysql_error()); 
$num = mysql_num_rows($result);

//** Check that the entry exists otherwise send to error page
if ($num > 0) {
  $row = mysql_fetch_array($result);
  echo $headline = $row['headline'];
} else {
  header("Location: error.php");
  exit;
}

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.