Somi Rana Somi Rana - 1 month ago 8
PHP Question

how to get search value from data base using php if 2 or 3 words same to stored data base value

my code is fine and its working well but i have to write same term as its stored in data base
like if i want to search a value named"awais123" which is stored in data base i have to write same term as "awais123"
if i will write awa or aws it will return nothing

//my datauser.php

<form action="data2.php" method="post" class="navbar-form navbar-right">
<div class="input-group">
<input type="Search" name="user_name" value="" placeholder="Search..." class="form-control" />
<div class="input-group-btn">
<button class="btn btn-info">
<span class="glyphicon glyphicon-search"></span>
</button>
</div>
< /div>
</form>


//now my "data2.php"

<?php
include("db/opendb.php");
$username=$_POST['user_name'];

$query = "SELECT * FROM user WHERE user_name='".$username."'";
$result = $conn ->query($query) or die("Query error");
?>
<table width="983" border="1" cellpadding="5" class="insertTable">
<tr>

<td width="109"><strong>user name</strong></td>

<td width="38"><strong>user phone no</strong></td>
<td width="99"><strong>dept id</strong></td>
<td width="40"><strong></strong></td>
</tr>
<?php
foreach($result as $row)
{
?>

<tr>

<td><?php echo $row['user_name']; ?></td>

<td><?php echo $row['user_phone_no']; ?></td>
<td><?php echo $row['dept_id']; ?></td>
<td><a href="delete.php?id=<?php echo $row['user_name']; ?>">Delete</a> </td>
</tr>
<?php
}
?>


please help me i just want that if just 3 words are found in my term its display result

Answer

Change you $query to:-

$query = "SELECT * FROM user WHERE user_name LIKE '%".$username."'%";