在html表中显示php搜索结果

时间:2012-06-05 11:50:40

标签: php

我正在运行这个PHP脚本,并没有得到我想要的结果。目前它给我这个输出

  

潜水罐

     

麦克

     

0.00   450.00   5.00

     

2012-06-04 18:50:22

     

潜水罐

     

利安

     

80.00   350.00   2.50

     

2012-06-04 19:00:09

     

显示3个结果

     

潜水罐

     

约什

     

410.00   0.00   5.00

     

2012-06-04 19:00:09

它几乎是我想要的,除了显示3个结果的行应该显示在结尾而不是最后一个条目之前。我需要对我的脚本做些什么来解决这个问题?

<?php
    $host = "localhost";
    $user = "root";
    $pass = null;

    // ========================================
    $dbhost = @mysql_connect($host, $user, $pass) or die("Unable to connect to server");

    @mysql_select_db("divebay") or die("Unable to select database");

    $var = "scuba";
    $query = trim($var);

    if(!isset($query)){
        echo "Your search was invalid";
        exit;
    } //line 18

    $sql = "SELECT * FROM auction WHERE name LIKE '%" . $query . "%'";

    $result = mysql_query($sql);
    $numrows = mysql_num_rows($result);
    mysql_close($dbhost);

    if($numrows == 0){
        echo "Sorry, your search did not return any results";
    }

    $i = 0;

    while($i < $numrows){
        $row = mysql_fetch_array($result);

        $ID = $row['ID'];
        $name = $row['name'];
        $owner = $row['owner'];
        $holder = $row['holder'];
        $start = $row['sprice'];
        $current = $row['cprice'];
        $instant = $row['iprice'];
        $inc = $row['incprice'];
        $image = $row['img'];
        $time = $row['stime'];
        $length = $row['duration'];

        echo "
            <?xml version = '1.0' encoding = 'utf-8'?>
            <!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>
                <title>searchdbresults</title>
            </head>

            <body>  
                <table style='width = 800px;'>
                    <tr style ='height = 200px;'>
                        <td style ='width = 200px;'></td>

                        <td style ='width = 300px;'>
                            <div style ='180px'> $name </div>
                            <div> $owner </div>
                        </td>

                        <td style='width =200px'>
                            <div style='height = 100px'> $current </div>
                            <div style='height = 50px'> $instant </div>
                            <div> $inc </div>
                        </td>

                        <td> $time </td>
                    </tr>
        ";

        $i++;   
    }

    echo "
                <tr> Displaying $numrows results</tr>
            </table>
        </body>
        </html>
    ";
?>

4 个答案:

答案 0 :(得分:1)

我无法发表评论,但您的代码中存在一些问题。

第一种风格必须加倍引用

<div style="width:100%;"> 

例如。

第二:此行中的tr内必须有一个td:显示$ numrows结果

最后一个我看到的是:你有这个:

<?xml version = '1.0' encoding = 'utf-8'?>
<!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>
<title>searchdbresults</title>
</head>
<body>  

在你的while循环中,所以在你的页面中多次,并且它不能

您还可以在while循环中打开表格,但不会关闭。所以它已经开了几次,但只开了一次。

编辑:您还需要在sql查询中添加保护

答案 1 :(得分:1)

您的脚本正在生成“混乱”的HTML。从我看到你生成的HTML页面(在当前示例中)将有3个DOCTYPE定义,3个head以及3个开放table标记,只有一个结束/table。而且你不需要echo每个单独的html实体,你可以在php文件中使用普通的html 试试这样的事情:

<?xml version = '1.0' encoding = 'utf-8'?>
<!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>
<title>searchdbresults</title>
</head>
<body>
<?php

$host = "localhost";
$user = "root";
$pass = null;

// ========================================
$dbhost = @mysql_connect($host, $user, $pass) or die("Unable to connect to server");

@mysql_select_db("divebay") or die("Unable to select database");

$var = "scuba";
$query = trim($var);

if(!isset($query)){
    echo "Your search was invalid";
    exit;
} //line 18

$sql = "SELECT * FROM auction WHERE name LIKE '%" . $query . "%'";

$result = mysql_query($sql);
$numrows = mysql_num_rows($result);
mysql_close($dbhost);

if($numrows == 0){
    echo "Sorry, your search did not return any results";
}
else{
?>
<table style='width = 800px;'>
<?php 
    $i = 0;

    while($i < $numrows){
    $row = mysql_fetch_array($result);

    $ID = $row['ID'];
    $name = $row['name'];
    $owner = $row['owner'];
    $holder = $row['holder'];
    $start = $row['sprice'];
    $current = $row['cprice'];
    $instant = $row['iprice'];
    $inc = $row['incprice'];
    $image = $row['img'];
    $time = $row['stime'];
    $length = $row['duration'];
?>
<tr style ="height: 200px;">
<td style ="width: 200px;"></td>
<td style ="width: 300px;">
    <div style ="width: 180px"><?php echo $name; ?></div>
    <div><?php echo $owner; ?></div>
</td>
<td style="width: 200px;">
    <div style="height: 100px;"><?php echo $current; ?></div>
    <div style="height: 50px;"><?php echo $instant; ?></div>
    <div><?php echo $inc; ?></div>
</td>
<td><?php echo $time; ?></td>
</tr>
<?php
      i++;
    } //end of while
} //end of else
?>
<tr>
    <td colspan="4">Displaying <?php echo $numrows; ?> results</td>
</tr>
</table>
</html>

并且还考虑阻止SQL注入:http://bobby-tables.com/

答案 2 :(得分:0)

如果你将你的php和html分成单独的文件,我认为你会好得多。你似乎正在失去对你开放的追踪。并关闭&#34;。如果你想要你的

<tr> Displaying $numrows results</tr>

在页面底部,然后将其从表格中删除。

答案 3 :(得分:0)

我建议稍微简化你的表格,也许完全从表格中取出“显示$ numrows结果”。

第'&lt; tr&gt;行显示$ numrows结果&lt; / tr&gt;'是无效的HTML。 &LT; TR&GT;表示“定义新表格行”,但需要&lt; td&gt;在里面包装内容。由于表的大多数行包含多个TD元素,而此行仅包含一条信息,因此您需要将该表格单元格告知span multiple columns

调试此类事情的一种好方法是将生成的HTML提供给http://validator.w3.org/,或者用示例数据替换$ variables并将模板代码提供给验证器。这通常有点令人沮丧(因为它会强迫您准确了解您想要使用的HTML版本等),但这是一种跟踪问题的好方法。如果您将以下HTML代码段提供给W3验证程序,它将为您提供一些有用的反馈:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>A</title></head>
<body>

<table><tr><td>aaa</td><td>bbb</td></tr>
<tr><td>cccc</td><td>dddd</td></tr>
<tr>Test</tr>
</table>
</body>
</html>

验证器告诉您:第7行,第5列:此处不允许使用字符数据

<tr>Test</tr>

换句话说,TR元素不应直接包含文本。

它还说:第7行,第13列:“tr”的结束标记未完成

<tr>Test</tr>

“最有可能的是,你嵌套标签并以错误的顺序关闭它们......另一种可能性是你使用的元素需要你没有包含的子元素。因此父元素是”未完成“,不是例如,在HTML中,&lt; head&gt;元素必须包含&lt; title&gt;子元素,列表需要适当的列表项(&lt; ul&gt;和&lt; ol&gt; require&lt; li&gt; ...),等等上“。

一旦您想要静态HTML查找和验证,就可以重新添加PHP循环,但这次您可以将脚本输出与工作HTML示例进行比较。

相关问题