简体   繁体   中英

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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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