开发者

Why does this PHP script interfere with my CSS layout?

开发者 https://www.devze.com 2022-12-31 13:46 出处:网络
This page uses $_GET to grab an asset id and query a mysql database and return some information. If \'id\' does not match anything, no results are displayed but the page looks fine.If \'id\' is null

This page uses $_GET to grab an asset id and query a mysql database and return some information.

If 'id' does not match anything, no results are displayed but the page looks fine. If 'id' is null an error would occur at $id = $_GET["id"] or die(mysql_error()); When this occurs, they page layout is not displayed correctly. How do I fix this?

Bonus question: How would I get a message like "No matching results found" or something when the id does not match any id in the database or is null.

Thank you.

<!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>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><li><a href="index.php">Search Assets</a></li></td>
                                <td><li><a href="browse.php">Browse Assets</a></li></td>
                                <td><li><a href="add_asset.php">Add Asset</a></li></td>
                                <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                                </tr>
                                </table>
                          </ul>
                        </div>
                        <div id="text">
                        <ul>
                        <li>
                        <h1>View Asset</h1>
                        </li>
                        </ul>
<table width="100%" border="0" cellpadding="2">
<?php

//make database connect
mysql_connect("localhost", "asset_db", "asset_db") or die(mysql_error());
mysql_select_db("asset_db") or die(mysql_error());

//get asset
$id = $_GET["id"] or die(mysql_error());
//get type of asset
$sql = "SELECT asset.type
From asset
WHERE asset.id = $id";
$result = mysql_query($sql)
or die(mysql_error());
$row = mysql_fetch_assoc($result);
$type = $row['type'];

switch ($type){
case "Server":
$sql = "
SELECT asset.id
,asset.company
,asset.location
,asset.purchase_date
,asset.purchase_order
,asset.value
,asset.type
,asset.notes
,server.manufacturer
,server.model
,server.serial_number
,server.esc
,server.user
,server.prev_user
,server.warranty
FROM asset
LEFT JOIN server
    ON server.id = asset.id
WHERE asset.id = $id
";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Asset ID:</td><td>";
    $id = $row['id'];
    setcookie('id', $id);
    echo "$id</td></tr>";
    echo "<tr<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Company:</td><td>";
    $company = $row['company'];
    setcookie('company', $company);
    echo "$company</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Location:</td><td>";
    $location = $row['location'];
    setcookie('location', $location);
    echo "$location</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Purchase Date:</td><td>";
    $purchase_date = $row['purchase_date'];
    setcookie('purchase_date', $purchase_date);
    echo "$purchase_date</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Purchase Order:</td><td>";
    $purchase_order = $row['purchase_order'];
    setcookie('purchase_order', $purchase_order);
    echo "$purchase_order</td></tr>";
    echo "<tr><t开发者_JAVA技巧d>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Value:</td><td>";
    $value = $row['value'];
    setcookie('value', $value);
    echo "$value</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Type:</td><td>";
    $type = $row['type'];
    setcookie('type', $type);
    echo "$type</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Notes:</td><td>";
    $notes = $row['notes'];
    setcookie('notes', $notes);
    echo "$notes</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Manufacturer:</td><td>";
    $manufacturer = $row['manufacturer'];
    setcookie('manufacturer', $manufacturer);
    echo "$manufacturer</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Model / Description:</td><td>";
    $model = $row['model'];
    setcookie('model', $model);
    echo "$model</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Serial Number / Service Tag:</td><td>";
    $serial_number = $row['serial_number'];
    setcookie('serial_number', $serial_number);
    echo "$serial_number</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Express Service Code:</td><td>";
    $esc = $row['esc'];
    setcookie('esc', $esc);
    echo "$esc</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>User:</td><td>";
    $user = $row['user'];
    setcookie('user', $user);
    echo "$user</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Previous User:</td><td>";
    $prev_user = $row['prev_user'];
    setcookie('prev_user', $prev_user);
    echo "$prev_user</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Warranty:</td><td>";
    $warranty = $row['warranty'];
    setcookie('warranty', $warranty);
    echo "$warranty</td></tr>";
}

break;


case "Laptop":
$sql = "
SELECT asset.id
,asset.company
,asset.location
,asset.purchase_date
,asset.purchase_order
,asset.value
,asset.type
,asset.notes
,laptop.manufacturer
,laptop.model
,laptop.serial_number
,laptop.esc
,laptop.user
,laptop.prev_user
,laptop.warranty
FROM asset
LEFT JOIN laptop
    ON laptop.id = asset.id
WHERE asset.id = $id
";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Asset ID:</td><td>";
    $id = $row['id'];
    setcookie('id', $id);
    echo "$id</td></tr>";
    echo "<tr<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Company:</td><td>";
    $company = $row['company'];
    setcookie('company', $company);
    echo "$company</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Location:</td><td>";
    $location = $row['location'];
    setcookie('location', $location);
    echo "$location</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Purchase Date:</td><td>";
    $purchase_date = $row['purchase_date'];
    setcookie('purchase_date', $purchase_date);
    echo "$purchase_date</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Purchase Order:</td><td>";
    $purchase_order = $row['purchase_order'];
    setcookie('purchase_order', $purchase_order);
    echo "$purchase_order</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Value:</td><td>";
    $value = $row['value'];
    setcookie('value', $value);
    echo "$value</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Type:</td><td>";
    $type = $row['type'];
    setcookie('type', $type);
    echo "$type</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Notes:</td><td>";
    $notes = $row['notes'];
    setcookie('notes', $notes);
    echo "$notes</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Manufacturer:</td><td>";
    $manufacturer = $row['manufacturer'];
    setcookie('manufacturer', $manufacturer);
    echo "$manufacturer</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Model / Description:</td><td>";
    $model = $row['model'];
    setcookie('model', $model);
    echo "$model</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Serial Number / Service Tag:</td><td>";
    $serial_number = $row['serial_number'];
    setcookie('serial_number', $serial_number);
    echo "$serial_number</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Express Service Code:</td><td>";
    $esc = $row['esc'];
    setcookie('esc', $esc);
    echo "$esc</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>User:</td><td>";
    $user = $row['user'];
    setcookie('user', $user);
    echo "$user</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Previous User:</td><td>";
    $prev_user = $row['prev_user'];
    setcookie('prev_user', $prev_user);
    echo "$prev_user</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Warranty:</td><td>";
    $warranty = $row['warranty'];
    setcookie('warranty', $warranty);
    echo "$warranty</td></tr>";
}

        break;  
case "Desktop":
$sql = "
SELECT asset.id
,asset.company
,asset.location
,asset.purchase_date
,asset.purchase_order
,asset.value
,asset.type
,asset.notes
,desktop.manufacturer
,desktop.model
,desktop.serial_number
,desktop.esc
,desktop.user
,desktop.prev_user
,desktop.warranty
FROM asset
LEFT JOIN desktop
    ON desktop.id = asset.id
WHERE asset.id = $id
";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Asset ID:</td><td>";
    $id = $row['id'];
    setcookie('id', $id);
    echo "$id</td></tr>";
    echo "<tr<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Company:</td><td>";
    $company = $row['company'];
    setcookie('company', $company);
    echo "$company</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Location:</td><td>";
    $location = $row['location'];
    setcookie('location', $location);
    echo "$location</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Purchase Date:</td><td>";
    $purchase_date = $row['purchase_date'];
    setcookie('purchase_date', $purchase_date);
    echo "$purchase_date</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Purchase Order:</td><td>";
    $purchase_order = $row['purchase_order'];
    setcookie('purchase_order', $purchase_order);
    echo "$purchase_order</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Value:</td><td>";
    $value = $row['value'];
    setcookie('value', $value);
    echo "$value</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Type:</td><td>";
    $type = $row['type'];
    setcookie('type', $type);
    echo "$type</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Notes:</td><td>";
    $notes = $row['notes'];
    setcookie('notes', $notes);
    echo "$notes</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Manufacturer:</td><td>";
    $manufacturer = $row['manufacturer'];
    setcookie('manufacturer', $manufacturer);
    echo "$manufacturer</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Model / Description:</td><td>";
    $model = $row['model'];
    setcookie('model', $model);
    echo "$model</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Serial Number / Service Tag:</td><td>";
    $serial_number = $row['serial_number'];
    setcookie('serial_number', $serial_number);
    echo "$serial_number</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Express Service Code:</td><td>";
    $esc = $row['esc'];
    setcookie('esc', $esc);
    echo "$esc</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>User:</td><td>";
    $user = $row['user'];
    setcookie('user', $user);
    echo "$user</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Previous User:</td><td>";
    $prev_user = $row['prev_user'];
    setcookie('prev_user', $prev_user);
    echo "$prev_user</td></tr>";
    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Warranty:</td><td>";
    $warranty = $row['warranty'];
    setcookie('warranty', $warranty);
    echo "$warranty</td></tr>";
}
        break;  
}

?>
</table>
<br />
<br />
<table width="100%" border="0">
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><a href="#">Add Software</a></td>
<td><a href="#">Edit Asset</a></td>
<td><a href="#">Delete Asset</a></td>
</tr>
</table>
                        </div>

                        </div>
                </div>
                <div class="clear"></div>
                <div id="footer" align="center">
                    <p>&nbsp;</p>
                </div>
                </div>
                <div id="tagline">

                </div>


</body>
</html>


You are very limited when you integrate code and markup this way. The approach that you should take is to run your code, process your inputs and generate a suitable output which then is passed to a template that knows how to output this data.

You can then check for a mysql error before you output anything, set a variable that if present and output a user friendly error message. If no error, then you can loop through your data to display your results.

If you want to loop through the data only once (vs. loop through the dataset first to build the array then again in the template loop), you can assign the mysql result to a variable and process it that way(though you can't close the db until you are done). It doesn't really make that much difference unless you are trying to save milliseconds in an enterprise application, or have 10s of 1000s of rows of data.

Either way you choose to do it, then only way to handle cases like this is to "process first, then present"... this gives you the maximum flexibility for how to handle output.


I'm going to be brutally honest here: your code is a mess; this is why PHP get a really bad rap. You really, really should look into separating your PHP and HTML.

The main issue is this:

$id = $_GET["id"] or die(mysql_error());

You don't even attempt to handle the condition when an id is not given; not to mention that mysql_error() would be an inappropriate function to call.

Instead, you should use a control structure to handle the situation when the id is not set.


if(!isset($_GET['id'])) {

    $result = mysql_query($sql);

    print '<p>Your search returned ';
    print mysql_num_rows($result);
    print 'results. </p>'; 

}

else {

    print '<p>An id was not supplied.</p>';
}


Just skimming through your code, you might want to fix this broken <tr>:

echo "<tr<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>Company:</td><td>";
         ^


Your layout breaks because you "die" right in the middle of outputting HTML. Your page will output the content up to the table just after "View Asset", then if the script dies, it will stop right there and not output a single character below. You'll end up with a table start tag being output but not closed, nor will any of the divs, the body or html tags be closed.

You'll also find that you cannot use the setcookie function once any HTML has started to be output.

I'd suggest completely separating your PHP logic and HTML output - preferably into separate files, or alternatively PHP code at the top of the file and HTML below. Do your logic and save your variables in PHP first, then output all your PHP normally, just including the appropriate variables where necessary. Something like this:

<?php
// connect to the database and run your query here
// save results into an array, something like this:
$rows = array();
while($row = mysql_fetch_assoc($result))
{
    // processing with $row here if needed
    $rows[] = $row;
}

//rest of PHP code

?>
<!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">
<!-- rest of the opening HTML code here -->

<table width="100%" border="0" cellpadding="2">
<?php foreach ( $rows as $row ) : ?>
<tr>
    <td>Addet ID:</td>
    <td><?php echo $row['id'] ?></td>
</tr>
<tr>
...etc
</tr>
<?php endforeach; ?>
</table>

<!-- rest of the closing HTML code here -->


You can't just die in the middle of the page - your page will be incomplete and invalid. What you need to do is to check your condition before opening table, output error message html in case of error and the table with content otherwise. Suicide is never a solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消