2

I've been learning PHP in my free time for a couple of weeks and this is the first problem I have had to ask help with. I've searched all over the internet but have found nothing I can fully understand when it comes to having a PHP mysql search engine with two input fields.

I have a search engine, which has two input fields, one field is for entering the kind of business one is looking for. The second field is for entering the location which one is trying to find the business in.

Could someone awesome and highly intelligent please tell me how do I get:

The first input field to search through my business column in my MySql table & the second input field to search through my location column in my MySql table.

I am using the FULLTEXT search engine method.

Here is the form snippet: (I know it's really sloppy, and probably unsafe but I plan on fixing that later.)

<form id="tfnewsearch" method="get" action="search.php">
   <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
         <td>
            <table width="476" border="0" cellspacing="0" cellpadding="0">
               <tr valign="middle">
                  <td width="476" class="">
                     <div id="tfheader2">
                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                           <tr>
                              <td width="91%">
                                 <table width="472" border="0" cellspacing="0" cellpadding="0">
                                    <tr>
                                       <td width="4">&nbsp;</td>
                                       <td width="139"><input style="width:210px" type="text" id="tfq" class="tftextinput2" name="business" size="20" maxlength="40" value="Business e.g. Insurance" onClick="if(this.value == 'Business e.g. Insurance') this.value='';" /></td>
                                       <td width="5">&nbsp;</td>
                                       <td width="69"><input  style="width:210px" type="text" id="tfq2" class="tftextinput2" name="location" size="20" maxlength="40" value="Location e.g. Hamilton" onClick="if(this.value == 'Location e.g. Hamilton') this.value='';" /></td>
                                       <td width="4">&nbsp;</td>
                                       <td>
                                          <input style="width:40px" type="image" src="site_images/q.png" alt="Search" class="tfbutton2" name="submit" value="&gt;" />
                                       </td>
                                       <td width="10">&nbsp;</td>
                                    </tr>
                                 </table>
                              </td>
                           </tr>
                        </table>
                        <div class="tfclear"></div>
                     </div>
                  </td>
               </tr>
            </table>
         </td>
      </tr>
   </table>
</form>

Here is the php snippet:

$button = @$_GET['submit'];
$search1 = @$_GET['business'];
$search2 = @$_GET['location'];

$strSQL = "SELECT * FROM users WHERE region = $search1 AND city = $search2";

$query = mysqli_query($con, $strSQL);
while ($result = mysqli_fetch_array($query));

if ($result = mysqli_query($con, $strSQL))

    $foundnum = mysqli_num_rows($result);

if ($foundnum == 0)

If you need more info please let me know.

Dinesh Belkare
  • 639
  • 8
  • 24
Hoani
  • 23
  • 3
  • 1
    Your markup (html) has many unmatched tags. I notice some opening tags that are not closed (or vice versa). Please start there and format the markup so it is readable (I tried to for you but the mismatched tags made it difficult) – Christian Oct 15 '15 at 05:36
  • 1
    you are using mysqli in really wrong manner – NullPoiиteя Oct 15 '15 at 05:42

1 Answers1

0

You should use ticks (') when binding variables to your query.

$strSQL = "SELECT * FROM users WHERE region = '$search1' AND city = '$search2'";

And in order to get the results, you can fetch them using mysqli_fetch_array(). Here is an example:

$query = mysqli_query($con, $strSQL); /* EXECUTE FIRST THE QUERY */
while($result = mysqli_fetch_array($query)){
  echo $result["column1"]." ".$result["column2"]."<br>"; /* REPLACE NECESSARY COLUMN NAMES */
}

You should also consider using *_real_escape_string() to prevent some SQL injections before you bind them to your query.

$search1 = mysqli_real_escape_string($con, $_GET['business']);

Instead of value tags for your Business e.g. Insurance, you can just use placeholder so you won't have to include the onClick="" tag.

<input type="text" id="tfq" class="tftextinput2" name="business" size="20" maxlength="40" style="width:210px" placeholder="Business e.g. Insurance"/>

You should also look at prepared statement while you are at it.

Community
  • 1
  • 1
Logan Wayne
  • 6,001
  • 16
  • 31
  • 49
  • Thank you very much everyone, especially you at this time Logan, big help thank you. Now I just need to sort out my pagination, its no longer working. Ill try to fix it myself but will ask for help if I absolutely cant figure it out. Thank you. Peace! – Hoani Oct 15 '15 at 08:54