【问题标题】:PHP Search engine using two input fields使用两个输入字段的 PHP 搜索引擎
【发布时间】:2015-10-15 05:28:08
【问题描述】:

我已经在空闲时间学习了几个星期的 PHP,这是我不得不寻求帮助的第一个问题。我已经在整个互联网上进行了搜索,但在使用带有两个输入字段的 PHP mysql 搜索引擎时没有发现任何我可以完全理解的内容。

我有一个搜索引擎,它有两个输入字段,一个字段用于输入您正在寻找的业务类型。第二个字段用于输入试图在其中找到业务的位置。

请一位了不起且非常聪明的人告诉我如何获得:

在 MySql 表中搜索 business 列的第一个输入字段 & 在 MySql 表中搜索我的 location 列的第二个输入字段。

我正在使用FULLTEXT搜索引擎方法。

这里是sn-p的形式: (我知道这真的很草率,而且可能不安全,但我计划稍后修复。)

<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>

这里是 php sn-p:

$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)

如果您需要更多信息,请告诉我。

【问题讨论】:

  • 您的标记 (html) 有许多不匹配的标签。我注意到一些未关闭的开始标签(反之亦然)。请从那里开始并格式化标记以使其可读(我尝试为您这样做,但不匹配的标签使其变得困难)
  • 您使用 mysqli 的方式非常错误

标签: php mysql search input


【解决方案1】:

在将变量绑定到查询时,您应该使用记号 (')。

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

为了获得结果,您可以使用mysqli_fetch_array() 获取它们。这是一个例子:

$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 */
}

在将SQL injections 绑定到查询之前,您还应该考虑使用*_real_escape_string() 来阻止它们。

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

您可以只使用placeholder,而不是value 标记为您的Business e.g. Insurance,这样您就不必包含onClick="" 标记。

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

您还应该查看prepared statement

【讨论】:

  • 非常感谢大家,尤其是此时的Logan,大大的帮助谢谢。现在我只需要整理我的分页,它不再工作了。我会尝试自己修复它,但如果我绝对无法弄清楚它会寻求帮助。谢谢你。和平!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-24
  • 1970-01-01
  • 2013-04-07
  • 2018-12-20
  • 2011-05-09
  • 2013-09-03
  • 1970-01-01
相关资源
最近更新 更多