Jump to content

If record not found how do I display an error page?


stualk

Recommended Posts

I'm trying to return a "Not found" page if a record isn't found in my db. The first bit of my code works fine where if the record is found it goes to the correct page and displays the results, it's the second bit that doesn't. I used to use the following code, which worked fine until php5 came about! What can I do to make it work now in php5?

 

<?php
$connection = mysql_connect("localhost","user","pass") or die ("No Server");
$db = mysql_select_db("cards_db", $connection) or die("No DataBase");
$sql = "select * from cards where card_number='$card_number'";
$result = mysql_query($sql) or die("No Query");
$num = mysql_numrows($result);
      for ($i=1; $i<=1; $i++) {
      if ($row = mysql_fetch_array($result)) {
      $card_number = $row["card_number"];
if ($num == 1) {
header("Location: index_returned.php?card_number=$card_number");
}
else
{
header("Location: index_wrong.php");	
}	}	}
?>

Link to comment
Share on other sites

Also, where is $card_number coming from:

 

$sql = "select * from cards where card_number='$card_number'";

 

this should probably be $_GET['card_number'] or $_POST['card_number']

 

Note to sure why you have a for loop in your code.....

 

 

 

Link to comment
Share on other sites

card_number comes from a submission form on the previous page. There is one field labelled 'card_number' and when submitted it carries the value of that field across to select from the db. This works perfectly if the record exists and it displays the results. If it finds nothing it's not doing the last bit of my code. I have adjusted as you suggested but it still doesn't work. My code currently looks like this, anything else it could be?:

 

<?php
$card_number = $_GET['card_number'];

$connection = mysql_connect("localhost","user","pass") or die ("No Server");
$db = mysql_select_db("cards_db", $connection) or die("No DataBase");
$sql = "select * from cards where card_number='$card_number'";
$result = mysql_query($sql) or die("No Query");
$num = mysql_num_rows($result);
      for ($i=1; $i<=1; $i++) {
      if ($row = mysql_fetch_array($result)) {
      $card_number = $row["card_number"];
if ($num == 1) {
header("Location: index_returned.php?card_number=$card_number");
}
else
{
header("Location: index_wrong.php");	
}	}	}
?>

Link to comment
Share on other sites

This may do the trick - note it's not tested....

 

$connection = mysql_connect("localhost","user","pass") or die ("No Server");
mysql_select_db("cards_db", $connection) or die("No DataBase");

$sql = "select card_number from cards where card_number= '" . $_GET['card_number'] . "' limit 1";
$result = mysql_query($sql) or die("No Query");

if(mysql_num_rows($result) > 0) {
 $row = mysql_fetch_object($result);      
 header("Location: index_returned.php?card_number=$row->card_number");
} else {
 header("Location: index_wrong.php");
}	

 

;)

 

 

Link to comment
Share on other sites

that code looks good to me. it should work...

 

$connection = mysql_connect("localhost","user","pass") or die ("No Server");
mysql_select_db("cards_db", $connection) or die("No DataBase");

$sql = mysql_query("SELECT * FROM `cards` WHERE `card_number`={$_GET['card_number']}");

echo mysql_num_rows($qry); //this is to see how much rows are returned.
exit(); //remove this when it is doing it properly.

if(mysql_num_rows($qry)  == 0) {
header("Location: index_wrong.php");
}else{
  $row = mysql_fetch_array($sql);      
  header("Location: index_returned.php?card_number={$row['card_number']}");
}

Link to comment
Share on other sites

Just a thought here.  Seems that you should be using everything in a single page here show your error in the same page you are sending the data to would simplify things a bunch.  Also if your server is ever down then using statements like exit; or die will cause the rest of the script not to colme to a halt.  If you use an error trap you just ignore the code that is meant for the query and then even redirect them to another error page or use that page to show the error.

 

Sample for your question above: (Untested!!)

 

<?php
$card_number = $_GET['card_number'];

$connection = mysql_connect("localhost","user","pass") or die ("No Server");
$db = mysql_select_db("cards_db", $connection) or die("No DataBase");
$sql = "select * from cards where card_number='$card_number'";
$result = mysql_query($sql) or die("No Query");
$num = mysql_numrows($result);
      for ($i=1; $i<=1; $i++) {
      if ($row = mysql_fetch_array($result)) {
      $card_number = $row["card_number"];
if ($num == 0) {
# redirection if $num = no result
header( 'Location: http://www.yoursite.com/new_page.html' ) ;
} else {
# otherwise redirect here
header("Location: index_returned.php?card_number=$card_number");
}
?>

Link to comment
Share on other sites

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.