我正在使用表单在数据库中搜索,我想知道如何在同一页面上显示搜索结果(页面可以刷新,我不介意)。
我的表单如下:
$ read var1 var2 var3 <<< "val1 val2 val3"; echo $var1
val1
search4.php是在数据库中进行搜索的脚本,如下所示:
<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中的结果。我会尝试这样的事情:
<?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";
}
?>
答案 0 :(得分:0)
您可以将搜索脚本放在同一页面上,然后将搜索表单定位到当前页面,并将脚本放在页面顶部。