【问题标题】:Combining a text box and a check box to create a search box结合文本框和复选框来创建搜索框
【发布时间】:2015-05-05 23:23:01
【问题描述】:

我想为我的网站搜索textbox 和checkbox 过滤器。搜索将搜索地点和位置的名称,而 checkbox 将过滤活动。

现在我有一个有效的搜索 textbox 和一个有效的 checkbox 过滤器,但无法弄清楚如何组合它们。

非常感谢任何帮助或指向教程等的链接,因为我仍在学习这两种语言,并且最近才开始挣扎,现在完全停滞不前。

感谢 kstro21,代码可以正常运行!

这是 PHP

<?php
  if(isset($_POST['submit'])){
  if(isset($_GET['go'])){
  if(preg_match("/^[  a-zA-Z]+/", $_POST['name'])){
  $name=$_POST['name'];
  //connect  to the database
  $db=mysql_connect  ("host", "dbo560757688",  "pw") or die ('I cannot connect to the database  because: ' . mysql_error());
  //-select  the database to use
  $mydb=mysql_select_db("db560757688");
 // Fruits
$fruits = array();
foreach($_POST['fruit'] as $fruit) {
    $fruit = mysql_real_escape_string($fruit);
    $fruits[] = "'{$fruit}'"; 
}
    //-query  the database table
  $sql="SELECT * FROM Contacts WHERE Activity IN (" . implode(", ", $fruits) . ") AND FirstName LIKE '%" . $name .  "%' OR LastName LIKE '%" . $name ."%'";
  //-run  the query against the mysql query function
  ?>
  <?php 
//omitted code

$result=mysql_query($sql);

//close your php just after this
  }
  }
  }
?>
<table border="1">
    <thead>
        <tr>
          <th>Name</th>
          <th>Last Name</th>
          <th>Activity</th>
        </tr>
    </thead>
    <tbody>
        <?php 
        //-create  while loop and loop through result set
          while($row=mysql_fetch_array($result)):
          $FirstName=$row['FirstName'];
          $LastName=$row['LastName'];
          $Activity=$row['Activity'];
          $Email=$row['Email'];
          //-display the result of the array
          ?>
        <tr>
            <td><?php echo $FirstName ?></td>
            <td><?php echo $LastName?></td>
            <td><?php echo $Activity ?></td>
        </tr>
      <?php endwhile; ?>
    </tbody>
</table>

HTML 代码

    <form  method="post" action="search.php?go"  id="searchform">
      <input  type="text" name="name">
      <br /> 
          <input type="checkbox" name="fruit[]" value="Paintball" /> 
          Banana<br />
    <input type="checkbox" name="fruit[]" value="Swimming Pool" /> Apple
    <br />
    <input type="checkbox" name="fruit[]" value="Gym" /> Peach
    <br />
      <input  type="submit" name="submit" value="Search">
    </form>
</form> 

【问题讨论】:

  • 您的代码看起来不错,您是否遇到任何错误?你的表单 html 是什么?
  • 现在将使用我的 HTML 进行更新,我没有收到任何错误,只是没有显示。
  • 如果你 var_dump 它包含任何内容吗? $fruits[] = "'{$fruit}'";

标签: php sql checkbox


【解决方案1】:

您使用 php 打印的 html 代码有一些问题,例如,table 标记未关闭,与第一个 tr 相同,此外,您在 for 中包含所有代码,所以,它会打印更多次,这是你想要的吗?

首先,我建议您尝试在$fruits[] = "'{$fruit}'"; 之后关闭for循环,然后再试一次

编辑:
我认为您的 sql 查询是错误的,请更改

WHERE Activity LIKE (" . implode(", ", $fruits) . ")";

为:

WHERE Activity IN (" . implode(", ", $fruits) . ")";

您不能将LIKE 与逗号分隔值一起使用,而是使用IN

你可以在同一个文件中混合 html 和 php 代码,所以你可以做这样的事情

<?php 
//...omitted code

$result=mysql_query($sql);

//close your php just after this
?>
<table border="1">
    <thead>
        <tr>
          <th>Name</th>
          <th>Last Name</th>
          <th>Activity </th>
        </tr>
    </thead>
    <tbody>
        <?php 
        //-create  while loop and loop through result set
          while($row=mysql_fetch_array($result)):
              $FirstName=$row['FirstName'];
              $LastName=$row['LastName'];
              $Activity=$row['Activity'];
              $Email=$row['Email'];
              //-display the result of the array
          ?>
            <tr>
                <td><?php echo $FirstName ?></td>
                <td><?php echo $LastName ?></td>
                <td><?php echo $Activity ?></td>
            </tr>
      <?php endwhile; ?>
    </tbody>
</table>

【讨论】:

  • 感谢您仍然在显示结果时遇到一些问题并出现“语法错误,文件意外结束”在第 50 行至于显示结果我想要一个可以显示所有结果的表格.再次感谢您的帮助,非常感谢我已经坚持了一个多星期!
  • 不,tbody 仍然导致错误,一旦我删除它,它仍然说最后有一个错误,一旦我删除该行,它就会继续发送错误,直到没有代码留下。
  • 请发布您遇到的错误,我可以在您编辑的问题中看到您的 for 未关闭,您缺少 }
  • 没有问题/语法错误,一切都已完全修复,但现在没有显示任何结果。
  • search.php 的第 11 行是什么?如果您更新了答案,我可以看到$sql 最后缺少),就在implode(", ", $fruits) . "" 之后应该是implode(", ", $fruits) . ")" 你注意到区别了吗?试试那个然后回击我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-13
  • 1970-01-01
  • 1970-01-01
  • 2011-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多