我正在使用textarea数据,这是一个低于彼此的“名字”,并试图在mysql数据库中搜索这些名称,如果在数据库中找到该名称,则显示完整信息。
这是到目前为止的代码,但仅适用于1行数据
<?php
session_start();
//error_reporting(E_ALL);
//ini_set("display_errors", 1);
?>
<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("|",explode("\r\n", $textarea));
$_SESSION['firstname']= $textarea;
}
if(($_SESSION['firstname']!=""))
{
$view = $db->query("SELECT * FROM contacts WHERE firstname REGEXP '$search'");
$check = $view;
if($check!="")
{
while($output = mysqli_fetch_array($view))
{
?>
<div class="reslt">
<h3 id="resuil-title">
<?php echo $output['firstname']; ?>
</h3>
<p class="Description">
<hr>
</div>
<?php } } else { ?>
<p class="error">
<?php echo $view; ?>
<p>
<?php } } ?>
</form>
</body>
</html>
我尝试使用“$ search = explode('\ n',$ _POST ['search']);”但不知何故无法使其工作或使用错误。谁能告诉我我做错了什么或错过了什么?
答案 0 :(得分:1)
尝试这样的事情
$textarea = trim($_POST['search']);
$search = implode("|",preg_replace("/[^a-zA-Z 0-9]+/", "",explode("\n", $textarea)));
,MySQL查询看起来像
if(($_SESSION['firstname']!=""))
{
$view = $db->query("SELECT * FROM contacts WHERE firstname REGEXP '$search'");
$check = mysqli_num_rows($view);
}