Jump to content

Redirect users to a new page


Mr Chris

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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.