PHP搜索关键字

时间:2014-02-02 06:10:41

标签: php mysql search

我有问题。无论如何要在我的PHP程序中搜索关键字。在数据库中的示例有一个全名Joe smith。如果我只搜索乔,它将不会出现?但是,如果我搜索乔史密斯确切地说它将是apear。我想看看所有有史密斯的名字。但它没有表现出来

<?php
session_start();

if($_SESSION['username'])
{

//Include the connection file
include "connection.php";

$sql = "SELECT * FROM users ";

if (isset($_POST['search'])) {

$search_term = mysql_real_escape_string($_POST['search_box']);

$sql .= "WHERE fullname = '{$search_term}' ";

$sql .= " OR username = '{$search_term}' ";

$sql .= " OR address = '{$search_term}' ";

$sql .= " OR id = '{$search_term}' ";


}

$query = mysql_query($sql) or die(mysql_error());

echo '<FORM METHOD="LINK" ACTION="member.php">
<INPUT TYPE="submit" VALUE="Back">
</FORM>';
echo '<FORM METHOD="LINK" ACTION="display_data.php">
<INPUT TYPE="submit" VALUE="Refresh">
</FORM>';

}
else
{
die("You must be logged in!");
}

?>
<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'>
</form>


<input type="button" onclick="printDiv('printableArea')" value="Print" />

<div id="printableArea">
<table width="70%" cellpadding="5" cellspace="5">

<tr>
    <td><strong>ID</strong></td>
    <td><strong>Fullname</strong></td>
    <td><strong>Username</strong></td>
    <td><strong>Address</strong></td>
</tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<tr>
    <td><?php echo $row['id']; ?></td>
    <td><?php echo $row['fullname']; ?></td>
    <td><?php echo $row['username']; ?></td>
    <td><?php echo $row['address']; ?></td>
</tr>

<?php }?>
</div>


</table>

<script>
function printDiv(divName) {
 var printContents = document.getElementById(divName).innerHTML;
 var originalContents = document.body.innerHTML;

 document.body.innerHTML = printContents;

 window.print();

 document.body.innerHTML = originalContents;
}
</script>
抱歉,我真的不是英语格瑞特,而是试着理解这句话。

我还可以询问如何使用Logi打印可打印区域吗? Tnx:))))

1 个答案:

答案 0 :(得分:0)

您可以尝试LIKE而不是=

实施例

SELECT aut_name, country FROM author WHERE aut_name LIKE 'W%';

并在您的代码中

$sql = "SELECT * FROM users ";

if (isset($_POST['search'])) {

$search_term = mysql_real_escape_string($_POST['search_box']);

$sql .= "WHERE fullname LIKE '%{$search_term}%' ";

$sql .= " OR username LIKE '%{$search_term}%' ";

$sql .= " OR address LIKE '%{$search_term}%' ";

$sql .= " OR id LIKE '%{$search_term}%' ";