我似乎无法工作的一件事是输出textarea字段,即使数据库中没有找到数据。 像这样:
数据库值:
现在,如果有人搜索:
输出应该看起来像这样:
我希望这在某种程度上是可以理解的
到目前为止这是我的代码
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<form method="post">
<textarea name="search" type="search" list="searchkey" placeholder="search" class="search"></textarea>
<input type="submit" name="submit" id="click" value="search" />
<?php
$db = new mysqli('localhost', 'root', 'root', 'skfstapel');
if(isset($_POST['submit']))
{
$textarea = trim($_POST['search']);
$search = implode("|",preg_replace("/[^a-zA-Z 0-9]+/", "",explode("\n", $textarea)));
$_SESSION['firstname']= $textarea;
}
if(($_SESSION['firstname']!=""))
{
$view = $db->query("SELECT * FROM contacts WHERE firstname REGEXP '$search'");
$check = mysqli_num_rows($view);
if($check!=""){
while($output = mysqli_fetch_array($view))
{
?>
<div class="reslt">
<?php echo $output['firstname']; ?>
<?php echo $output['lastname']; ?>
</h3>
<hr>
</div>
<?php } } } else { ?>
<p class="error">
Error: Add new data enter and check the correct keyword
<p>
<?php } ?>
</form>
</body>
</html>
我感谢任何帮助
答案 0 :(得分:1)
你应该循环关键字而不是sql结果:
...
if($check!=""){
$output = array();
while($row = mysqli_fetch_object($view)) {
$output[$row->firstname] = $row;
}
$keywords = explode("\n", $textarea);
foreach($keyword as $keywords){
if(array_keys_exists($keyword, $output){
...