I have made a page with a search bar. It searches for keywords from three columns in a table. Now the problem I am facing is, I want the table to appear after I search for a keyword. But what is happening is, the header of the table, is coming before the submission itself. And along with it, I get a notice and warning.
Notice: Undefined variable: result in C:\xampp\htdocs\sdis\data\search-2d-data.php on line 35 Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\xampp\htdocs\sdis\data\search-2d-data.php on line 35
Here is my code, can someone please help me.
<?php
include $_SERVER["DOCUMENT_ROOT"] . '/sdis/core/init.php';
include $_SERVER["DOCUMENT_ROOT"] . '/sdis/includes/overall/header.php';
if (isset($_POST['search']) === true) {
$search = $_POST['search'];
$query = "SELECT * FROM `2d_raw_data` WHERE `basin` LIKE '%" . $search . "%' OR `area` LIKE '%" . $search . "%' OR `block` LIKE '%" . $search . "%'";
$result = mysql_query($query);
}
?>
<form id="form1" name="form1" method="post" action="">
<h2><center>Search 2D Data</center></h2>
<input type="text" class="form-control" name="search" required/><br/>
<center><button class="btn btn-lg btn-primary" type="submit" value="Search">Search</button></center><br/>
</form><br/><br/>
<table width="100%" border="1" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="100%" border="1" cellspacing="1" cellpadding="3">
<tr>
<td colspan="50"><strong><center>2D Data</center></strong> </td>
</tr>
<tr>
<td align="center"><strong>Basin</strong></td>
<td align="center"><strong>Area</strong></td>
<td align="center"><strong>Block</strong></td>
</tr>
<?php
while ($rows = mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $rows['basin']; ?></td>
<td><?php echo $rows['area']; ?></td>
<td><?php echo $rows['block']; ?></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
Also, the current code only searches for a match from one column at a time, and if I put two words separated by a space, it doesn't search for two keywords across all columns, instead it returns 0. How do I search for multiple keywords across all columns and return a result?