Php 进一步筛选我的搜索

Php 进一步筛选我的搜索,php,sql,database,Php,Sql,Database,我想回显我想要的结果,如何进一步过滤它们 示例,搜索x100 y100,目前可获得数百个结果。我需要进一步过滤这些结果,这样我只会得到那些标记为敌对、待定或友好的结果 我有下面的html表单 <form action="xysearch.php" method="post"> <label>X Coord <input type="text" name="x" /> </label> <label

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

示例
搜索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>

X坐标
Y坐标
怀有敌意的
悬而未决的
友好的
我需要添加到搜索查询中的是一种过滤这些结果的方法,以便只显示在“外交”下拉列表中选择的选项

到目前为止,我的查询不起作用——我可以得到所有的结果,但不仅仅是过滤后的结果

<?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>";

    }

?>
使用:

“从我的_表中选择dip,其中dip位于“%”“$term.”“%”“ORDER BY.”“$term.”“DESC”

或者如果这不起作用:

“从我的_表中选择dip,其中dip=”“$term.”“ORDER BY”“。$term.”“DESC”

是的,SQL注入:)您非常容易受到攻击。

我查看了,它似乎只接受一个结果集,因此如果我是正确的,您只需要迭代第一个结果集,即未过滤的结果集

array mysql\u fetch\u array(resource$result[,int$result\u type=mysql\u BOTH])

为什么不在单个查询中筛选结果?看起来您是从单个表(my_表)中获取项目,对吗

以下是一份工作报告:


请注意,您将使用“|”(或)运算符连接不同的术语。

代码的第一眼:

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 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>";

    }

?>

另一个技巧是使用PDO。使用mysql函数并不难。在某种程度上,创建更好、更可读的代码更容易。

您的脚本似乎容易受到攻击。你应该看一看你好,谢谢你的建议,我理解风险,但是,该网站是为只有少数人访问它。此访问受密码保护。我确信,随着我对php了解的深入,我将采用PDO方法。再次感谢您的警告和建议如果您不想使用PDO方法,只需使用mysql\u real\u escape\u string即可。我已经尝试了您发送的内容(非常感谢),不幸的是,它没有工作。所有结果都来自同一个表,但列不同。能否粘贴到呈现的查询中?i、 e:有实值而不是变量的那个?我马上给你拿把SQL小提琴。另外,我已经编辑了查询,ORDER BY part应该使用列名而不是值。我已经稍微更改了查询并添加了一个工作的SQLFIDLE,如果有帮助,请告诉我。