0

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?

Pranav C Balan
  • 110,383
  • 23
  • 155
  • 178
Ankur Sinha
  • 6,165
  • 7
  • 38
  • 71

3 Answers3

1

No need for that weird true in your if:

  if(isset($_POST['search'])){

isset() itselfs returns true if parameter is set. If you haven't posted any data to that script, the query never runs and $result is never setted.

aksu
  • 5,163
  • 5
  • 22
  • 38
  • Is there anyway, I can display the table header and the data along with it after I click submit? Right now before the form submission, the table header is coming up and the data after submission. I want both to come after submission. How can I do that? – Ankur Sinha Dec 25 '13 at 12:26
1

In your while loop, you haven't returned a $result unless if(isset($_POST['search'])===true){ (ie when you first access the form before submitting), so you'll get this error unless you wrap that while loop in a similar if test

Mark Baker
  • 205,174
  • 31
  • 336
  • 380
  • Is there anyway, I can display the table header and the data along with it after I click submit? Right now before the form submission, the table header is coming up and the data after submission. I want both to come after submission. How can I do that? – Ankur Sinha Dec 25 '13 at 12:25
1

$result is initialized only if the condition if (isset($_POST['search']) === true) is met. Thus you'll have to add if (isset($result)) before the while loop.

When it comes to searching please check out the mysql manual for fulltext search.

Ahmed Siouani
  • 13,423
  • 12
  • 60
  • 72
Jakub Kania
  • 14,430
  • 2
  • 35
  • 44
  • Is there anyway, I can display the table header and the data along with it after I click submit? Right now before the form submission, the table header is coming up and the data after submission. I want both to come after submission. How can I do that? – Ankur Sinha Dec 25 '13 at 12:25
  • The code you included doesn't have any table header (th) tags so your question is pretty hazy. – Jakub Kania Dec 25 '13 at 16:16
  • The block which is marked strong with Basin, Area and Block. I meant that. My bad. – Ankur Sinha Dec 25 '13 at 16:38
  • Oh. So what you're saying is that you don't want the header to display if there is no data? Just put the entire table in that if instead of just the loop. – Jakub Kania Dec 25 '13 at 20:01