我有部分世界搜索的问题。
以下代码有效,但仅限全字搜索。如果我只将部分单词放在搜索框中,则它不起作用。请你能给我一些提示,以便能够进行部分单词搜索吗?
<?php include "connection.php";
$sql = "SELECT * FROM Klienci ";
if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= "WHERE nazwaKlienta = '{$search_term}' ";
$sql .= " OR adresMail = '{$search_term}' ";
$sql .= " OR adresMail2 = '{$search_term}' ";
}
$query = mysql_query($sql) or die(mysql_error());
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>BAZA | Dashboard</title>
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/jumbotron-narrow.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="header">
<ul class="nav nav-pills pull-right">
<li class="active"><a href="index.php">Home</a></li>
<li><a href="add_customer.php">Dodaj Firmę</a></li>
</ul>
<h3 class="text-muted">ITalents</h3>
</div>
<div class="row marketing">
<div class="col-lg-12">
<h2>KLIENCI</h2>
<table class="table table-striped">
<form name="search_form" method="POST" action="display_data.php">
Search: <input type="text" name="search_box" value ="" />
<input type="submit" name="search" value="Search the table..." >
</form>
<tr>
<th>id</th>
<th>Klient</th>
<th>Email</th>
<th>Email 2</th>
<th>Data dodania do bazy</th>
</tr>
<?php while ($row = mysql_fetch_array($query)) {
//Display customer info
$output ='<tr>';
$output .='<td>'.$row['id'].'</td>';
$output .='<td>'.$row['nazwaKlienta'].'</td>';
$output .='<td>'.$row['adresMail'].'</td>';
$output .='<td>'.$row['adresMail2'].'</td>';
$output .='<td>'.$row['dataDodania'].'</td>';
$output .='<td><a href="edit_customer.php?id='.$row['id'].'" class="btn btn-default btn-sm">Edit</a></td>';
$output .='<td><a href="delete_customer.php?id='.$row['id'].'" class="btn btn-default btn-sm">Delete</a></td>';
$output .='</tr>';
//Echo output
echo $output;
?>
<?php } ?>
</table>
</div>
</div>
</div>
<div class="footer">
<p>© Company 2014</p>
</div>
</div> <!-- /container -->
</body>
</html>
答案 0 :(得分:0)
以下代码应该有效。你必须使用LIKE而不是使用相等。例如:什么东西像'%something2%'
<?php include "connection.php";
$sql = "SELECT * FROM Klienci ";
if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= "WHERE nazwaKlienta LIKE '%{$search_term}%' ";
$sql .= " OR adresMail LIKE '%{$search_term}%' ";
$sql .= " OR adresMail2 LIKE '%{$search_term}%' ";
}
$query = mysql_query($sql) or die(mysql_error());
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>BAZA | Dashboard</title>
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/jumbotron-narrow.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="header">
<ul class="nav nav-pills pull-right">
<li class="active"><a href="index.php">Home</a></li>
<li><a href="add_customer.php">Dodaj Firmę</a></li>
</ul>
<h3 class="text-muted">ITalents</h3>
</div>
<div class="row marketing">
<div class="col-lg-12">
<h2>KLIENCI</h2>
<table class="table table-striped">
<form name="search_form" method="POST" action="display_data.php">
Search: <input type="text" name="search_box" value ="" />
<input type="submit" name="search" value="Search the table..." >
</form>
<tr>
<th>id</th>
<th>Klient</th>
<th>Email</th>
<th>Email 2</th>
<th>Data dodania do bazy</th>
</tr>
<?php while ($row = mysql_fetch_array($query)) {
//Display customer info
$output ='<tr>';
$output .='<td>'.$row['id'].'</td>';
$output .='<td>'.$row['nazwaKlienta'].'</td>';
$output .='<td>'.$row['adresMail'].'</td>';
$output .='<td>'.$row['adresMail2'].'</td>';
$output .='<td>'.$row['dataDodania'].'</td>';
$output .='<td><a href="edit_customer.php?id='.$row['id'].'" class="btn btn-default btn-sm">Edit</a></td>';
$output .='<td><a href="delete_customer.php?id='.$row['id'].'" class="btn btn-default btn-sm">Delete</a></td>';
$output .='</tr>';
//Echo output
echo $output;
?>
<?php } ?>
</table>
</div>
</div>
</div>
<div class="footer">
<p>© Company 2014</p>
</div>
</div> <!-- /container -->
</body>
</html>