我想创建搜索页面(php),我希望在“div”中显示我从数据库中搜索到的数据?我与数据库建立了连接并在一个phppage中搜索数据并在另一个phppage中创建了div标签。如何显示一个php页面的搜索数据,以显示在另一个php页面的“div”
中的search.php:
<?php
include 'Searchdata.php';
include 'connect.php';
if(isset($_POST['submit'])){
$searchkey= $_POST['search'];
$searchkey=preg_replace("#[^0-9a-z]#i", "", $searchkey);
$query = mysqli_query($conn, "SELECT * FROM newentry WHERE Date LIKE '%$searchkey%'")or die("Could not search!");
$count = mysqli_num_rows($query);
if(!($count == 0)) {
while($row=mysqli_fetch_array($query)){
$Date=$row['Date'];
$Entry=$row['Entry'];
echo'<div>'.$Date.'<br>'.$Entry.'</div>';
}
} else {echo "There was no search result!";}
}?>
Searchdata.php:
<div>
<form action="Search.php" method="post">
<input type="text" name="search" placeholder="Search">
<input type="submit" value="Search" />
</form>
答案 0 :(得分:0)
只需创建一个变量来存储结果,在这种情况下是$ data。将include searchdata.php移动到代码的底部,以便它可以重新计算$ data。 然后在你的html页面上回复它。
html page
<div>
<form action="Searchdata.php" method="post">
<input type="text" name="search" placeholder="Search">
<input type="submit" value="Search">
</form>
<div><?php echo $data ?></div>
</div>
PHP代码。
<?php
include 'connect.php';
$data = '';
if(isset($_POST['submit'])){
$searchkey= $_POST['search'];
$searchkey=preg_replace("#[^0-9a-z]#i", "", $searchkey);
$query = mysqli_query($conn, "SELECT * FROM newentry WHERE Date LIKE '%$searchkey%'")or die("Could not search!");
$count = mysqli_num_rows($query);
if(!($count == 0)) {
while($row=mysqli_fetch_array($query)){
$Date=$row['Date'];
$Entry=$row['Entry'];
$data = '<div>'.$Date.'<br>'.$Entry.'</div>';
}
} else {
$data = "There was no search result!";}}
include 'Search.php';
?>