2

我正在使用 PHP 调用数据库来打印 3 个不同的下拉菜单。这样可行。我的问题是调用函数并将下拉选择传递给函数并在按下提交按钮后显示记录。该函数是一个构建查询,考虑到是否只选择了下拉菜单中的 1 个或全部 3 个。

该功能当前与表单位于同一页面中。

这是表格:

 <form action="edit.php" method="POST">
 <select>
      <?php $getGroup = mysql_query("SELECT DISTINCT resgroup FROM restable ORDER BY               resgroup"); 
      while($viewAllGroups = mysql_fetch_array($getGroup)){
      ?>
      <option id="<?php echo $viewAllGroups['resgroup']; ?>"><?php echo $viewAllGroups['resgroup']; ?></option><?php } ?>
 </select>

 <select>
      <?php $getType = mysql_query("SELECT DISTINCT restype FROM restable ORDER BY restype");
      while($viewAllTypes = mysql_fetch_array($getType)){
      ?>
      <option id="<?php echo $viewAllTypes['restype']; ?>"><?php echo $viewAllTypes['restype']; ?></option><?php } ?>
 </select>

 <select>
     <?php $getService = mysql_query("SELECT DISTINCT service FROM restable ORDER BY service");
  while($viewAllServices = mysql_fetch_array($getService)){
  ?>
      <option id="<?php echo $viewAllServices['service']; ?>"><?php echo $viewAllServices['service']; ?></option><?php } ?>
 </select>
 <input type="submit" class="btn btn-primary" value="Filter" />
 </form>

这是功能:

 <?php
 function displayrecords(){
     $groups = $_POST['resgroup'];
     $type = $_POST['restype'];
     $service = $_POST['service'];

 if($groups != ""){
     $where[] = " `resgroup` = '".mysql_real_escape_string($group)."'";
 }
 if($type != ""){
     $where[] = " `restype` = '".mysql_real_escape_string($type)."'";
 }
 if($service != ""){
     $where[] = " `service` = '".mysql_real_escape_string($service)."'";
 }

 $sql_json = "SELECT * FROM mytable WHERE $where_clause ORDER BY id DESC";
 }
 ?>

然后我尝试显示该功能。

 <?php displayrecords(); ?>

我没有收到错误,但是,一旦单击提交按钮,下拉菜单就会清除,并且不会返回任何内容。我知道我错过了很多。我将不胜感激任何帮助。

先感谢您。

4

3 回答 3

1

首先,请为每个选择元素提供名称。再次在 edit.php 文件中通过该名称访问 post 数组的值。

现在我举一个例子。

HTML部分:

<select name='select1' >
  <option value='1'>Value</option>
  <option value='1'>Value</option>
</select>

现在在 edit.php 中,您可以访问选择框 select1 的选定元素的值$_POST['select1'];

于 2013-07-31T13:50:22.687 回答
1

您正在将一个数组添加到字符串中,这只会导致"SELECT * FROM mytable WHERE Array() ORDER BY id DESC";或类似的结果。

尝试在您的$sql_json = "...线路之前添加此内容:

$where = implode(" AND ", $where);

这应该将restype=value AND service=valueetc 添加到您的字符串中。

此外,您在您的子句中引用$group而不是。$groupsif($groups != "")

此外,您必须为您的select标签命名,以便能够在以下位置引用它们$_POST

<select name="restype">
于 2013-07-31T13:53:22.037 回答
0

您需要更改您的 PHP,因为 sql 语句正在寻找变量 $where_clause 并且我没有在您的代码中看到它定义。

你可以重写where子句的构建

<?php
 function displayrecords(){
     $groups = $_POST['resgroup'];
     $type = $_POST['restype'];
     $service = $_POST['service'];
 $where = "";
 if($groups != ""){
     $where = " `resgroup` = '".mysql_real_escape_string($group)."'";
 }
 if($type != ""){
     if( $where != "" ) $where .= " AND ";
     $where .= " `restype` = '".mysql_real_escape_string($type)."'";
 }
 if($service != ""){
     if( $where != "" ) $where .= " AND ";
     $where .= " `service` = '".mysql_real_escape_string($service)."'";
 }

 $sql_json = "SELECT * FROM mytable WHERE $where ORDER BY id DESC";
 }
 ?>
于 2013-07-31T13:52:16.000 回答