从多个选项调用函数

时间:2013-07-31 13:46:31

标签: php mysql function

我使用PHP来调用数据库来打印3个不同的下拉菜单。这样可行。我的问题是调用函数并将下拉选择传递给函数并在按下提交按钮后显示记录。该函数是一个构建查询,考虑是否只选择了一个dropwdowns或全部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(); ?>

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

提前谢谢。

3 个答案:

答案 0 :(得分:2)

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

现在我举一个例子。

HTML部分:

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

现在在edit.php中,您可以访问selectbox select1的selected元素的值 作为$_POST['select1'];

答案 1 :(得分:1)

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

尝试在$sql_json = "...行添加此内容:

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

这应该在您的字符串中添加restype=value AND service=value等。

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

此外,您必须为select代码添加一个名称,以便能够在$_POST中引用它们:

<select name="restype">

答案 2 :(得分: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";
 }
 ?>