How do I return a message if $_GET is null or does not match any database entries?
I am working on designing an IT Asset database. Here I am working on a page used to view details on a specific asset determined by an asset id.
Here I grab $id
from $_GET["id"];
开发者_StackOverflow
When $id
is null, the page does not load. When $id
does not match any entry within the database, the page loads but no asset table is printed.
In both these cases, I would like to display a message like "There is no database entry for that Asset ID"
How would this be handled? Thank you.
<?php
/*
* View Asset
*
*/
# include functions script
include "functions.php";
$id = $_GET["id"];
ConnectDB();
$type = GetAssetType($id);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title>Wagman IT Asset</title>
</head>
<body>
<div id="page">
<div id="header">
<img src="images/logo.png" />
</div>
</div>
<div id="content">
<div id="container">
<div id="main">
<div id="menu">
<ul>
<table width="100%" border="0">
<tr>
<td width="15%"></td>
<td width="30%%"><li><a href="index.php">Search Assets</a></li></td>
<td width="30%"><li><a href="addAsset.php">Add Asset</a></li></td>
<td width="25%"></td>
</tr>
</table>
</ul>
</div>
<div id="text">
<ul>
<li>
<h1>View Asset</h1>
</li>
</ul>
<br />
<?php
switch ($type){
case "Server":
$result = QueryServer($id);
$ServerArray = GetServerData($result);
PrintServerTable($ServerArray);
break;
case "Desktop";
break;
case "Laptop";
break;
}
?>
</div>
</div>
</div>
<div class="clear"></div>
<div id="footer" align="center">
<p> </p>
</div>
</div>
<div id="tagline">
Wagman Construction - Bridging Generations since 1902
</div>
</body>
</html>
<?php if (empty($type)): ?>
<!-- print your message -->
<?php else: ?>
<!-- show asset stuff -->
<?php endif; ?>
If $id is null then you shouldn't connect to/query the database at all. Also, another style option using a ternary to check for empty.
$id = !empty($_GET["id"]) ? $_GET["id"] : '';
if ($id) {
ConnectDB();
$type = GetAssetType($id);
}
then, same as webbidave's solution in the HTML to show message
精彩评论