简体   繁体   English

PHP搜索关键字

[英]PHP search keyword

I have a problem. 我有个问题。 is there anyway to search a key word in my PHP program. 无论如何,有没有在我的PHP程序中搜索关键字。 Example if in the database There is a Full name Joe smith. 数据库中是否有全名Joe smith的示例。 And if i search Joe only it will not appear?? 如果我仅搜索Joe,它将不会出现?? But if i search Joe Smith exact it will apear. 但是,如果我精确搜索乔·史密斯,它将会出现。 i want to see all the names that have a smith in it. 我想查看其中有史密斯的所有名称。 but its not showing it 但它没有显示

<?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>

sorry im really not GReat in english but try to understand this Sentences. 对不起,我真的不懂英语,但请尝试理解这句话。

can i also ask on how to print the printable AREA with Logi in it? 我还能问一下如何使用Logi打印可打印的区域吗? Tnx :)))) Tnx :))))

you may try LIKE instead of = . 您可以尝试使用LIKE代替=

Example; 例;

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

and in your code 并在您的代码中

$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}%' "; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM