Jabir Jabir - 6 months ago 18
MySQL Question

php search is not displaying the database record

I have a database created in phpmyadmin, with few columns and data inserted.Right now i can display the table with record in the browser. But what i want is to display a particular value by filtering from the database.the following is the piece of code i have written. At this moment if I input any value into the search field it doesn't sort out, and do nothing.

<?php
if(isset ($_POST['search']))
{
$valueToSearch=$_POST['valueToSearch'];
//$query="SELECT * FROM `books` WHERE CONCAT (`ISBN`, `Title`, `Author`)LIKE '%".$valueToSearch."%'";
$query="SELECT * FROM books WHERE CONCAT ('ISBN', 'Title', 'Author')LIKE '%".$valueToSearch."%'";
$serach_result=filterTable($query);
}else{
$query="SELECT * FROM books";
$serach_result=filterTable($query);
}

function filterTable($query){

$connect=mysqli_connect ("localhost", "root", "", "bookstore-new");
$filter_result=mysqli_query($connect, $query);
return $filter_result;
}
?>
<!DOCTYPE html>

<html>
<head>

<title>Lab 4</title>

<style>
table, tr, th, td
{
border: 1px solid black;
}
</style>
</head>
<body>
<form action="bookstorewebdev.php" method="post">

<input type="text" name="valueToSearch" placeholder="value to search" >
<br><br>
<input type="submit" name="Search" value="Filter" ><br><br>
<table>
<tr>
<th>ISBN</th>
<th>Title</th>
<th>Author</th>
</tr>
<?php while ($row=mysqli_fetch_array($serach_result)):?>
<tr>
<td>&nbsp;<?php echo $row['ISBN'];?></td>
<td>&nbsp;<?php echo $row['Title'];?></td>
<td>&nbsp;<?php echo $row['Author'];?></td>
</tr>
<?php endwhile;?>

</table>
</form>
</body>
</html>

Answer

1 - In HTML form you have :

<input type="submit" name="Search" value="Filter">

and code in which you are checking variable : search.

Search != search

2 - You not check the variable valueToSearch. It is can be empty. Request will fail.

3 - About concat you said chris85. Why do you need this?