简体   繁体   English

使用PHP在表的同一页面上显示搜索结果

[英]Display search results on the same page in a table with PHP

I am using a form to search in a database and I would like to know how to display the search results in a table, on the same page (the page can refresh, I don't mind). 我正在使用一种表单在数据库中进行搜索,并且我想知道如何在同一页面的表中显示搜索结果(该页面可以刷新,我不介意)。

My form looks like this: 我的表格如下所示:

<form id="searchform"  method="post" action = 'search4.php' target = '_blank'>
  <input id="name" style="height: 25px; width: 140px; position: fixed; top: 150px; left: 50px" name="name" type="text" >
  <input type="submit" value="Search" class="btn btn-primary btn" style="color: white; font-style: normal; background-color: blueviolet; position: fixed; top: 148px; left: 220px">
</form>

search4.php is the script that does the searching in the database and looks like this: search4.php是在数据库中执行搜索的脚本,如下所示:

<?php
$servername = 'localhost';
$username   = 'root';
$password   = '';
$dbname     = 'official_db';

$mysqli = new mysqli($servername, $username, null, $dbname);
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}  

if (!get_magic_quotes_gpc() ) {
    $Name = addslashes($_POST['name']);
} else {
    $Name = $_POST['name'];
}

session_start();
$results = "SELECT * FROM b2b_interfaces WHERE Name LIKE CONCAT ('%', $name, '%')";

$resultSet = $mysqli->query($results);
$numRows = $resultSet->num_rows;
if ($numRows > 0) {
    while ($row = $resultSet->fetch_object()) {
        echo "{$row->name} {$row->address} {$row->county}  <br>";
    }
} else {
    echo "No Results";
}
?>

In the main script I also have defined a table, but I do not know how to have access to the results from search4.php. 在主脚本中,我还定义了一个表,但是我不知道如何访问search4.php的结果。 I would try something like this: 我会尝试这样的事情:

<tbody>
      <?php
      if ($numRows > 0) {
          while ($row = $resultSet->fetch_object()) {
      ?>
          <tr>
            <td><?php echo "{$row->name} " ?></td>
            <td><?php echo "{$row->address} " ?></td>
            <td><?php echo "{$row->county} " ?></td>
          </tr>
      <?php
          }
      }
      ?>
</tbody>

您可以将搜索脚本放置在同一页面上,因此将搜索表单定位到当前页面,然后将脚本放置在页面顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM