FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Display an error if there is no value in the database





pashmina
/productdetail.php?CategoryID=

in the page- productdetail.php, if the value of the CategoryID is not mentioned, the data fields on the page displays blank. its the same case if there is no such id in the database.
this is obvious, so i want to display an error if the value of the CategoryID= is not mentioned or it is not available in the Database.

which is to show

Error!! no record found

instead of

Code:
<p>Category Name<strong><?php echo $row_Recordset1['CategoryName']; ?></strong>
</p>
<p>Description <strong><?php echo $row_Recordset1['CategoryDesc']; ?></strong></p>



btw, i am using dreamweaver for this project

productdetail.php
Code:
<?php require_once('Connections/handicraft.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_Recordset1 = "-1";
if (isset($_GET['CategoryID'])) {
  $colname_Recordset1 = $_GET['CategoryID'];
}
mysql_select_db($database_handicraft, $handicraft);
$query_Recordset1 = sprintf("SELECT * FROM category WHERE CategoryID = %s", GetSQLValueString($colname_Recordset1, "int"));
$Recordset1 = mysql_query($query_Recordset1, $handicraft) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Product Detail</title>
</head>

<body>
<p>Category Name<strong><?php echo $row_Recordset1['CategoryName']; ?></strong>
</p>
<p>Description <strong><?php echo $row_Recordset1['CategoryDesc']; ?></strong></p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>
kv
Code:

<?
if($colname_Recordset1!="-1" AND isset($row_Recordset1) )
{
?>
<body>
<p>Category Name<strong><?php echo $row_Recordset1['CategoryName']; ?></strong>
</p>
<p>Description <strong><?php echo $row_Recordset1['CategoryDesc']; ?></strong></p>
</body>
<?
}
else
{
   echo "Error!! no record found";
}
?>
pashmina
it worked perfectly just one bracket ")" was missing in your code.

but still if you enter the url http://127.0.0.1/handicraft/productdetail.php?CategoryID=
it does not give out that error.. is there any way to fix that as well?
jmraker
Try changing

Code:
<?
if($colname_Recordset1!="-1" AND isset($row_Recordset1)
{


to
Code:
<?
if($colname_Recordset1!="-1" AND $row_Recordset1 != null)
{
?>

and URLs that contain the IP number 127.0.0.1 doesn't point to your server, for everyone it resolves to their computer, for me it points to my webserver, for you it points to yours, for everyone else it'll point to their probably non-existant web server.
rvec
Shocked

try this:
Code:
<?php require_once('Connections/handicraft.php');

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_Recordset1 = (int) $_GET['CategoryID'];
if ($colname_Recordset1>0)
{
   mysql_select_db($database_handicraft, $handicraft);
   $query_Recordset1 = "SELECT * FROM category WHERE CategoryID = $colname_Recordset1";
   $Recordset1 = mysql_query($query_Recordset1, $handicraft) or die(mysql_error());
   $row_Recordset1 = mysql_fetch_assoc($Recordset1);
   $totalRows_Recordset1 = mysql_num_rows($Recordset1);
   $body = <<<HTML
<body>
<p>Category Name<strong>{$row_Recordset1['CategoryName']}</strong>
</p>
<p>Description <strong>{$row_Recordset1['CategoryDesc']}</strong></p>
</body>
HTML;
   mysql_free_result($Recordset1);
} else
{
   $body = "<body>No records found!</p></body>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Product Detail</title>
</head>
<?php echo $body; ?>
</html>
jmraker
That assumes that any number in $_GET['CategoryID'] is a record in the database beforehand. If CategoryID=1 isn't in the database, it wouldn't show the no record error.

The variable $totalRows_Recordset1 should contain the number of records with that ID, either a 0 (if there are no records) or 1+ for the number of records.

Code:

if(is_numeric($_GET['CategoryID'])){
  $colname_Recordset1 = (int)$_GET['CategoryID'];
  mysql_select_db($database_handicraft, $handicraft);
  $query_Recordset1 = "SELECT * FROM category WHERE CategoryID = $colname_Recordset1";
  $Recordset1 = mysql_query($query_Recordset1, $handicraft) or die(mysql_error());
  $totalRows_Recordset1 = mysql_num_rows($Recordset1);
  if ($totalRows_Recordset1 > 0)
  {
     $row_Recordset1 = mysql_fetch_assoc($Recordset1);
     ...
  }
  else{
  // no record
  }
}
else {
// ID is not valid
}
pashmina
rvec wrote:
Shocked

try this:
Code:
<?php require_once('Connections/handicraft.php');

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_Recordset1 = (int) $_GET['CategoryID'];
if ($colname_Recordset1>0)
{
   mysql_select_db($database_handicraft, $handicraft);
   $query_Recordset1 = "SELECT * FROM category WHERE CategoryID = $colname_Recordset1";
   $Recordset1 = mysql_query($query_Recordset1, $handicraft) or die(mysql_error());
   $row_Recordset1 = mysql_fetch_assoc($Recordset1);
   $totalRows_Recordset1 = mysql_num_rows($Recordset1);
   $body = <<<HTML
<body>
<p>Category Name<strong>{$row_Recordset1['CategoryName']}</strong>
</p>
<p>Description <strong>{$row_Recordset1['CategoryDesc']}</strong></p>
</body>
HTML;
   mysql_free_result($Recordset1);
} else
{
   $body = "<body>No records found!</p></body>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Product Detail</title>
</head>
<?php echo $body; ?>
</html>

Thank you for the code, but it is too advance for me. it would have been better if the body part was mention as it is, in that way its easy to edit it on dreamweaver. with the technique above it doesn't give any preview on dreamweaver.

@jmraker
Thanks for the

Code:
<?
if($colname_Recordset1!="-1" AND isset($row_Recordset1)

code, it worked.

Code:
  }
  else{
  // no record
  }
}
else {
// ID is not valid
}

i would like to do this in a easy way.. the new code is

Code:
<?php require_once('../Connections/world.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_code = "-1";
if (isset($_GET['code'])) {
  $colname_code = $_GET['code'];
}
mysql_select_db($database_world, $world);
$query_code = sprintf("SELECT * FROM country WHERE Code = %s", GetSQLValueString($colname_code, "text"));
$code = mysql_query($query_code, $world) or die(mysql_error());
$row_code = mysql_fetch_assoc($code);
$totalRows_code = mysql_num_rows($code);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Country Info</title>
</head>
<?
if($colname_code!="-1" AND $row_code != null)
{
?>
<body>
<p>Code:<strong><?php echo $row_code['Code']; ?></strong> also <strong><?php echo $row_code['Code2']; ?></strong>
<p>
  Country:
  <strong><?php echo $row_code['Name']; ?></strong>
<p>
  Continent:
  <strong><?php echo $row_code['Continent']; ?></strong>
<p>
  Region:
  <strong><?php echo $row_code['Region']; ?></strong>
<p>
  Surface Area:
  <strong><?php echo $row_code['SurfaceArea']; ?></strong>
<p>
  In dept Year:<strong><?php echo $row_code['IndepYear']; ?></strong>
<p>
  Population:
  <strong><?php echo $row_code['Population']; ?></strong>
<p>
  Life Expectancy:
  <strong><?php echo $row_code['LifeExpectancy']; ?></strong>
<p>
  GNP:
  <strong><?php echo $row_code['GNP']; ?></strong>
  previously- <strong><?php echo $row_code['GNPOld']; ?></strong>
<p>
  Local Name: <strong><?php echo $row_code['LocalName']; ?></strong>
<p>
  Government Form:
  <strong><?php echo $row_code['GovernmentForm']; ?></strong>
<p>
  Head of the state: <strong><?php echo $row_code['HeadOfState']; ?></strong>
<p>
  Capital: <strong><?php echo $row_code['Capital']; ?></strong>
<p>
</body>
<?
}
else
{
   echo "Error!! no record found";
}
?>
</html>
<?php
mysql_free_result($code);
?>
Related topics
IE 6 Display Error
Error Restoring database Details That database does not be
Database connection class!
Database Error: Unable to connect to the database HELP
error connecting database
Internal server error and database not found
PHP DB Error: syntax error
Java tutorials
URGENT HELP NEEDED FOR PHPBB
SQL error on my site
MySQL error
need help restoring phpbb database from old webhost.
a site where i can learn mysql syntax
selecting database
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.