0

这是修改后的代码。还是没有运气!请。帮助

<html>
<body>

<?php

 $mysqli = new mysqli("localhost", "root", "password", "test");

 $whereClauses = '';
 $numLocations = count($_POST['Locations']);
 $numJobs = count($_POST['Jobs']);
 $i = 0;
 if (! empty($_POST['Locations'])) {
  foreach ($_POST['locations'] as $location) {
    $whereClauses .="Locations='".mysql_real_escape_string($location)."'";
    if ($i++ == $numLocations) {
     $whereClauses .= " AND";
    }
  }
  }
  if (! empty($_POST['Jobs'])) {
  foreach ($_POST['Jobs'] as $job) {
    $whereClauses .="Jobs='".mysql_real_escape_string($job)."'";
  }
  if ($i++ == $numJobs) {
     $whereClauses .= " AND";
  }
   }
  $sql = "SELECT * FROM mytable '".$whereClauses."' ORDER BY id DESC '".$limit."'";

$result=mysql_query($sql);

while ($row = mysql_fetch_array($result)) {
echo $row['Locations'];
echo $row['Jobs'];
}

?>
 </body>
</html>

================================================= 我创建了一个HTML 和 PHP 文件,用于根据多个下拉过滤器过滤 Web 表单数据。这是表格。当我运行表单和 PHP 时,我在浏览器中看不到任何结果。也没有错误。我正在研究另一个论坛成员发布的示例。请帮忙。提前致谢。

<form action="showJobs_new.php" method="post">
<select name="Locations">
<option value="" selected="selected">All Locations</option>
<option value="arizona">Arizona</option>
<option value="alaska">Alaska</option>
</select>
<select name="Jobs">
<option value="" selected="selected">All jobs</option>
<option value="Carpenter">Carpenters</option>
<option value="Plumbers">Plumbers</option>
</select>
<input type="submit" value="search jobs" />
</form>

showJobs_new.php:

<html>
<body>

<?php

$username="root";
$password="password";
$database="test";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$whereClauses = array(); 
if (! empty($_POST['Locations'])) $whereClauses[] ="Locations='".mysql_real_escape_string($_POST['Locations'])."'"; 
if (! empty($_POST['Jobs'])) $whereClauses[] ="Jobs='".mysql_real_escape_string($_POST['Jobs'])."'"; 
$where = ''; 
if (count($whereClauses) > 0) { $where = ' WHERE '.implode(' AND ',$whereClauses); } 

$sql = mysql_query("SELECT * FROM mytable ORDER BY id DESC $limit" .$where);  

$result=mysql_query($sql);
or die("Error: ".mysql_error()."<br />Query: ".$sql);

while ($row = mysql_fetch_assoc($result)) {
echo $row['Locations'];
echo $row['Jobs'];
}

?>
</body>
</html>
4

2 回答 2

3

将查询更改为

$sql = mysql_query("SELECT * FROM mytable " .$where." ORDER BY id DESC $limit");  

查询的顺序似乎是错误的。

于 2012-10-22T12:12:11.133 回答
1

嗨亲爱的你有两个查询错误。

1-这是因为您输入了错误的子句顺序。您需要了解查询中子句的优先级是什么。运行查询时,子句的顺序应如下所示

  • 选择

  • 在哪里

  • 通过...分组

  • 订购方式

  • 限制

2-按照建议使用 mysql_query 两次user1599669

所以你的查询应该看起来像

 $sql = mysql_query("SELECT * FROM mytable $where ORDER BY id DESC $limit");

如需更多参考,请阅读mysql dev

于 2012-10-22T12:12:17.980 回答