2013-04-07 30 views
0

我想回显我想要的结果,我如何进一步过滤它们?进一步过滤我的搜索

示例,search x100 y100,目前可获得数百个结果。我需要过滤这些结果进一步所以我只让那些这就是被标记为敌对的,尚未了结或友好

我有以下的HTML表单

<form action="xysearch.php" method="post"> 
    <label>X Coord 
     <input type="text" name="x" /> 
     </label> 
    <label>Y Coord 
     <input type="text" name="y" /> 
     </label> 
    <select name="term"> 
     <option value="Hostile">Hostile</option> 
     <option value="Pending">Pending</option> 
     <option value="Friendly">Friendly</option> 
     </select> 
    <input type="submit" value="Search" /> 
    </form> 

什么,我需要添加到搜索查询办法过滤这些结果,所以只显示在外交下拉列表中选择的选项

我的查询到目前为止,这是行不通的 - 我可以得到所有的结果,但不只是过滤的结果。

<?php 

$x = $_POST['x']; 
$y = $_POST['y']; 
$term = $_POST['term']; 

mysql_connect ("localhost","host","pass") or die (mysql_error()); 
mysql_select_db ("d_base"); 

$res = mysql_query("SELECT * FROM my_table WHERE (x BETWEEN $x -75 AND $x +75) AND (y BETWEEN $y -75 AND $y +75) "); 
$res2 = mysql_query("SELECT dip FROM my_table WHERE dip IN '%$term%' ORDER BY '%$term%' DESC "); 

    echo "<table border='1' align='center' cellpadding='5'>"; 
    echo "<tr> <th>City Name</th> <th>X</th> <th>Y</th> <th>Diplomacy</th> </tr>"; 

// loop through results of database query, displaying them in the table 

    while($row = mysql_fetch_array($res, $res2)) { 

// echo out the contents of each row into a table 

    echo '<td>' . $row['city'] . '</td>'; 
    echo '<td>' . $row['x'] . '</td>'; 
    echo '<td>' . $row['y'] . '</td>'; 
    echo '<td>' . $row['dip'] . '</td>'; 
    echo "</tr>"; 

// close table> 

    echo "</table>"; 

    } 

?> 

我也不太清楚,我要去的地方错了,因为我实际上可以得到结果呼应,仅仅是过滤这就是问题。

+0

你的脚本似乎很容易[SQL注入](http://en.wikipedia.org/wiki/SQL_injection)。你应该看看[如何防止SQL注入PHP?](http://stackoverflow.com/q/60174/53114) – Gumbo 2013-04-07 08:23:38

+0

你好,感谢您的意见,我了解风险,但是,网站只有少数人可以使用它。此访问受密码保护。我敢肯定,当我学习更多关于PHP的时候,我会采用PDO方法。再次,感谢您的警告和建议 – 2013-04-07 08:28:52

+0

如果您不想通过PDO方法,只需使用mysql_real_escape_string – sagibb 2013-04-07 08:32:06

回答

0

我看了看PHP definition of mysql_fetch_array,它似乎只接受一个结果集,所以如果我是正确的,那么您只是遍历第一个结果集,它是未经过滤的结果集。

array mysql_fetch_array (resource $result [, int $result_type = MYSQL_BOTH ])

你为什么不过滤在一个单一的查询结果呢?它看起来好像是从单个表(my_table)中获取项目,对吗?

这里是一个工作sqlfiddle

$res = mysql_query("SELECT * FROM my_table WHERE (x BETWEEN $x -75 AND $x +75) AND (y BETWEEN $y -75 AND $y +75) AND dip REGEXP '$term' ORDER BY dip DESC"); 
while($row = mysql_fetch_array($res)) { 
... 
} 

注意,你必须通过串联不同的条款的“|” (或)运营商。

+0

我已经尝试过您发送的内容(非常感谢),但它不起作用。所有结果来自同一个表格但列不同。 – 2013-04-07 08:35:07

+0

你能否粘贴在呈现的查询中?即:具有真实价值而不是变量的那个?我会在一分钟内给你一个SQL小提琴。另外,我编辑了查询,ORDER BY部分应该使用列名而不是值。 – sagibb 2013-04-07 08:37:31

+0

我已经改变了一下查询并添加了一个工作sqlfiddle,让我知道它是否有帮助。 – sagibb 2013-04-07 08:52:21

0

用途:

"SELECT dip FROM my_table WHERE dip IN '%".$term."%' ORDER BY '".$term."' DESC"

或者,如果不工作:

"SELECT dip FROM my_table WHERE dip='".$term."' ORDER BY '".$term."' DESC"

,是的,SQL注入:)你是非常脆弱的。

0

你的代码的第一印象:

while($row = mysql_fetch_array($res, $res2)) { 

是不正确的,因为$ RES2应该是一个与resultType,而不是从一个查询的结果。

的选项有:

while($row = mysql_fetch_array($res, MYSQL_ASSOC)) { 
while($row = mysql_fetch_array($res, MYSQL_NUM)) { 
while($row = mysql_fetch_array($res, MYSQL_BOTH)) { 

我想你想实现这样的事情:

$res = mysql_query("SELECT * FROM my_table WHERE (x BETWEEN $x -75 AND $x +75) AND (y BETWEEN $y -75 AND $y +75) AND dip LIKE '%$term%'"); 

这意味着这样的代码:

<?php 

$x = $_POST['x']; 
$y = $_POST['y']; 
$term = $_POST['term']; 

mysql_connect ("localhost","host","pass") or die (mysql_error()); 
mysql_select_db ("d_base"); 


$res = mysql_query("SELECT * FROM my_table WHERE (x BETWEEN $x -75 AND $x +75) AND (y BETWEEN $y -75 AND $y +75) AND dip LIKE '%$term%'"); 



    echo "<table border='1' align='center' cellpadding='5'>"; 
    echo "<tr> <th>City Name</th> <th>X</th> <th>Y</th> <th>Diplomacy</th> </tr>"; 

// loop through results of database query, displaying them in the table 

    while($row = mysql_fetch_array($res, MYSQL_ASSOC)) { 

// echo out the contents of each row into a table 

    echo '<td>' . $row['city'] . '</td>'; 
    echo '<td>' . $row['x'] . '</td>'; 
    echo '<td>' . $row['y'] . '</td>'; 
    echo '<td>' . $row['dip'] . '</td>'; 
    echo "</tr>"; 

// close table> 

    echo "</table>"; 

    } 

?> 

对于更好的数据库,我认为你应该将这些条款存储在一个单独的表格中,并进行一些连接:

如果你决定这样做,在一个类似的方式使用查询:

$res = mysql_query("SELECT * FROM my_table mt LEFT JOIN terms t ON (mt.term_id = t.id) WHERE (x BETWEEN $x -75 AND $x +75) AND (y BETWEEN $y -75 AND $y +75) AND dip LIKE '%$term%'"); 

另一个技巧是使用PDO。使用mysql_ * - 函数确实不难。以某种方式创建更好,更易读的代码更容易。