为什么这个PHP脚本会干扰我的CSS布局?

时间:2010-05-19 18:01:04

标签: php mysql html css layout

此页面使用$_GET获取资产ID并查询mysql数据库并返回一些信息。

如果'id'与任何内容都不匹配,则不会显示任何结果,但页面看起来很好。如果'id'为null,则$id = $_GET["id"] or die(mysql_error());会发生错误。发生这种情况时,页面布局无法正确显示。我该如何解决这个问题?

奖金问题:如果ID与数据库中的任何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>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><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 "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>

5 个答案:

答案 0 :(得分:5)

当您以这种方式集成代码和标记时,您非常有限。您应采取的方法是运行代码,处理输入并生成合适的输出,然后将其传递给知道如何输出此数据的模板。

然后,您可以在输出任何内容之前检查mysql错误,设置一个变量(如果存在)并输出用户友好的错误消息。如果没有错误,那么您可以遍历数据以显示结果。

如果你只想循环数据一次(首先通过数据集循环然后再在模板循环中构建数组),你可以将mysql结果赋给变量并以这种方式处理它(尽管你在完成之前无法关闭数据库)。除非您尝试在企业应用程序中保存毫秒,或者拥有10个1000行的数据,否则它实际上并没有太大的区别。

无论哪种方式你都选择这样做,那么处理这类案件的唯一方法是“先处理然后再处理”......这为你提供了处理输出的最大灵活性。

答案 1 :(得分:4)

我在这里要粗暴地说:你的代码很乱;这就是为什么PHP得到一个非常糟糕的说唱。你真的应该考虑分离你的PHP和HTML。

主要问题是:

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

当没有给出id时,你甚至都没有尝试处理这个条件;更不用说mysql_error()将是一个不适合调用的函数。

相反,您应该使用控制结构来处理未设置id的情况。


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

答案 2 :(得分:4)

只需浏览代码,您可能需要修复此损坏的<tr>

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

答案 3 :(得分:3)

您的布局会中断,因为您在输出HTML时会“死”。您的页面将在“查看资源”之后将内容输出到表格,然后如果脚本终止,它将在那里停止,而不是在下面输出单个字符。您最终会输出一个表格开始标记,但不会关闭,也不会关闭任何div,正文或html标记。

您还会发现,任何HTML开始输出后都无法使用setcookie功能。

我建议将PHP逻辑和HTML输出完全分开 - 最好分成单独的文件,或者文件顶部的PHP代码和下面的HTML。执行您的逻辑并首先在PHP中保存变量,然后正常输出所有PHP,只需在必要时包含适当的变量。像这样:

<?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 -->

答案 4 :(得分:2)

您不能只是在页面中间死亡 - 您的页面将不完整且无效。您需要做的是在打开表之前检查您的状况,如果出现错误则输出错误消息html,否则输出错误信息表。自杀永远不是解决方案。