如何在特定div中显示mysql表?

时间:2013-04-02 11:56:48

标签: php mysql html-table

我有一个用户搜索pdf的页面,结果显示在该页面中,但是在特定的div中...显示了该表,但页面的整个设计被破坏了...有人能指出我吗?我在这里做错了什么?

    <div id="content">
   <div class="post">
        <div class="entry">
        <form name="search" action="" method="post">
        <input type="text" class="search" name="searchbox" id="searchbox" />
        <input type="submit" name="search" id="search" value="Search" />
        </form>

        </div>
    </div>

    <div class="post">
         <div class="entry">
         <?php
              if(isset($_REQUEST['search'])){
                  $searchterm = $_REQUEST['searchbox'];
                  $sql = "select name,size from ebooks where name like '%$searchterm%'";
                  $result=mysql_query($sql);
                  if(!($result)){
                     die("Query to show fields from table failed");
                  }
                  $fields_num = mysql_num_fields($result);
                  echo "<table border='1'><tr>";
                  // printing table headers
                  for($i=0; $i<$fields_num; $i++){
                      $field = mysql_fetch_field($result);
                      echo "<td>{$field->name}</td>";
                  }
                  echo "</tr>\n";
                  // printing table rows
                  while($row = mysql_fetch_row($result)){
                  echo "<tr>";
                  // $row is array... foreach( .. ) puts every element
                  // of $row to $cell variable
                  foreach($row as $cell)
                  echo "<td>$cell</td>";
                  echo "</tr>\n";
                  }
                  mysql_free_result($result);

              }
         ?>
         </div>

    </div>

1 个答案:

答案 0 :(得分:1)

你的桌子没有关闭</table>没有?

这里有正确的代码:

<?php
if (isset($_REQUEST['search'])) {

    $searchterm = $_REQUEST['searchbox'];
    $sql = "SELECT `name`, `size` FROM `ebooks` WHERE `name` LIKE '%$searchterm%'";
    $result = mysql_query($sql);

    if (!($result)){
        die("Query to show fields from table failed");
    }

    $fields_num = mysql_num_fields($result);
    echo "<table border=\"1\">\n<tr>";

    for ($i=0; $i<$fields_num; $i++) {
        $field = mysql_fetch_field($result);
        echo "<td>{$field->name}</td>";
    }

    echo "</tr>\n";

    while ($row = mysql_fetch_row($result)) {
        echo "<tr>";
        foreach($row as $cell) {
            echo "<td>$cell</td>";
        }
        echo "</tr>\n";
    }

    echo "</table>\n";

    mysql_free_result($result);
}
?>