Okay, here’s the deal, I have a database let’s say with 2 columns:
“name” and “lastname”
When I type in a input box a name like “John” it would retrieve all John’s from the “name” column. When I type in a lastname like “Doe” it would retrieve all Doe’s from the “lastname” column. This works as expected.
But I would like it to also retrieve data from both columns when i type in “John Doe”, so it would return all John Doe’s. When typing one word in the searchbar, it works alright, but when i put in another word after space, it does not do what I want, coz I don’t kno how should i form the query right.
Here’s the code of query.php:
<?php
require_once("config.php");
// Fetch the data
$searchbar = $_POST['searchbar'];
$querymilion = "SELECT * from lms.customers where `lastname` LIKE '%$searchbar%' OR `name` LIKE '%$searchbar%'";
mysql_query('SET NAMES \'utf8\'');
$resultmilion = @mysql_query($querymilion);
// Return the results, loop through them and echo
if($fraza) {
while($rowmilion = mysql_fetch_array($resultmilion))
{
?>
<a href="javascript:klientid()" onClick="klientid()">
<table id="tablesz" >
<tr>
<td style="font-weight:bold; width: 160px; text-align:right;">ID:</td>
<td style="width:200px;" id="getidfromsearch"><?php echo $rowmilion['id']; ?></td>
<td style="font-weight:bold; width: 50px; text-align:right;">PESEL:</td>
<td style="width:200px;"><?php echo $rowmilion['ssn']; ?></td>
</tr>
<tr>
<td style="font-weight:bold;width: 160px; text-align:right;">Nazwisko i Imię:</td>
<td style="width:200px;"><?php echo $rowmilion['lastname']; ?> <?php echo $rowmilion['name']; ?></td>
<td style="font-weight:bold;width: 50px; text-align:right;">Dow.os.:</td>
<td style="width:200px;"><?php echo $rowmilion['icn']; ?></td>
</tr>
<tr>
<td style="font-weight:bold; width: 160px; text-align:right;">Adres instalacji</td>
<td style="width:200px;"><?php echo $rowmilion['address']; ?></td>
<td style="font-weight:bold; width: 50px; text-align:right;">Miasto:</td>
<td style="width:200px;"><?php echo $rowmilion['zip']; ?> <?php echo $rowmilion['city']; ?>
</td>
</tr>
</table>
</a>
<?php
}
}
?>
And here’s the searchbar:
<div id="szukaj">
<table id="tablesz" >
<tr>
<td class="naglowek">Szukaj klienta</td>
</tr>
<tr>
<td>
<form id="myForm" enctype="multipart/form-data" action="query.php" method="post" name="myForm">
<input type="text" name="searchbar" value="" />
<input type="submit" value="Szukaj" />
</form>
</td>
</tr>
<tr>
<td>
<span id="error"></span>
<span id="result"></span>
</td>
</tr>
</table>
</div>
This can become very slow when your table increases. But it works.
If you also want to have the possibility to switch the order of firstname lastname to lastname firstname, just add this to the Query: