Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何阻止URL查询字符串重复?_Php_Mysql_Url_Parameter Passing - Fatal编程技术网

Php 如何阻止URL查询字符串重复?

Php 如何阻止URL查询字符串重复?,php,mysql,url,parameter-passing,Php,Mysql,Url,Parameter Passing,我正在使用URL查询字符串过滤MySQL搜索结果。当用户单击其中一个链接时,查询连同另一个变量一起传递给应用程序,然后应用程序构建并执行对数据库的SQL查询 过滤有效-用户可以按类型、平台进行过滤,也可以同时进行排序,但问题是每次单击链接时,都会在$query末尾添加另一个变量 例如,如果用户在示例中键入并选择“作为过滤器”,则查询如下所示: keywords=example&genre=action keywords=example&genre=action&genr

我正在使用URL查询字符串过滤MySQL搜索结果。当用户单击其中一个链接时,查询连同另一个变量一起传递给应用程序,然后应用程序构建并执行对数据库的SQL查询

过滤有效-用户可以按类型、平台进行过滤,也可以同时进行排序,但问题是每次单击链接时,都会在
$query
末尾添加另一个变量

例如,如果用户在示例中键入并选择“作为过滤器”,则查询如下所示:

keywords=example&genre=action
keywords=example&genre=action&genre=adventure
但假设他们点击冒险类型,查询结果如下:

keywords=example&genre=action
keywords=example&genre=action&genre=adventure
如果变量已设置,是否有方法让查询替换变量

$query = mysql_real_escape_string(htmlentities(trim($_SERVER[QUERY_STRING]))); 

<div id="filter_nav">
        <ul  id="nav_form">
            <li><h3 id="h3">Genre: &nbsp;</h3>
            <li><a href="search.php?'.$query.'&genre=Fighting">Fighting</a></li>
            <li><a href="search.php?'.$query.'&genre=Role-Playing">Role-Playing</a></li>
            <li><a href="search.php?'.$query.'&genre=Action">Action</a></li>
        </ul>
        <ul  id="nav_form">
            <li><h3 id="h3">Platform: &nbsp;</h3>
            <li><a href="search.php?'.$query.'&platform=Playstation 3">PS3</a></li>
            <li><a href="search.php?'.$query.'&platform=xbox 360">Xbox 360</a></li>
            <li><a href="search.php?'.$query.'&platform=Gamecube">Gamecube</a></li>
        </ul>
        </div>
        ';
        echo '
        <ul  id="sorting_form">
            <li><h3 id="h3">SORT BY: &nbsp;</h3>
            <li><a href="search.php?'.$query.'&order=title">Title</a></li>
            <li><a href="search.php?'.$query.'&order=release_date">Date</a></li>
            <li><a href="search.php?'.$query.'&order=rating">Rating</a></li>

        </ul>
        ';

function search_results($keywords){
$returned_results = array();
$where = "";

$keywords = preg_split('/[\s]+/', $keywords);
$total_keywords = count($keywords);

foreach($keywords as $key=>$keyword){
    $where .= "title LIKE '%$keyword%'";
    if($key != ($total_keywords - 1)){
        $where .= " AND ";
    }   
}
if (isset($_GET['platform']) && !empty($_GET['platform'])){
    $platform = mysql_real_escape_string(htmlentities(trim($_GET['platform']))); 
        $where .= " AND platform='$platform'";
    }

if (isset($_GET['genre']) && !empty($_GET['genre'])){
    $genre = mysql_real_escape_string(htmlentities(trim($_GET['genre']))); 
                $where .= " AND genre='$genre'";
}

if (isset($_GET['order']) && !empty($_GET['order'])){
    $order = mysql_real_escape_string(htmlentities(trim($_GET['order'])));
    $where .= " ORDER BY $order DESC";
    }

$results ="SELECT * FROM games WHERE $where ";
$query=mysql\u real\u escape\u string(htmlentities(trim($\u SERVER[query\u string]));
  • 类型:
  • 站台:
'; 回声'
  • 排序方式:
'; 函数搜索结果($keywords){ $returned_results=array(); $where=“”; $keywords=preg_split('/[\s]+/',$keywords); $total_keywords=计数($keywords); foreach($keywords作为$key=>$keyword){ $where.=“类似“%$keyword%”的标题”; 如果($key!=($total_关键字-1)){ $where.=“和”; } } if(设置($\u GET['platform'])和&!empty($\u GET['platform'])){ $platform=mysql\u real\u escape\u字符串(htmlentities(trim($\u GET['platform'])); $where.=“平台=”$platform'; } 如果(isset($\u GET['genre'])和&!empty($\u GET['genre'])){ $genre=mysql\u real\u escape\u string(htmlentities(trim($\u GET['genre'])); $where.=“和GREEP=”$GREEP'; } if(设置($\u GET['order'])和&!空($\u GET['order'])){ $order=mysql\u real\u escape\u字符串(htmlentities(trim($\u GET['order'])); $where.=“按$ORDER DESC订购”; } $results=“从游戏中选择*WHERE$WHERE”;
修改每个


例如,删除“
”.$query.
部分。

使用您的代码,可以使用对每个链接进行解构和重建查询字符串

然而,就我个人而言,我会选择一个带有3个选择框的表单,其中预先选择了先前选择的值

您可以将所有选择选项放在一个多维数组中,并进行双循环

例如:

<?php
$options = array(
  "genre" => array("Fighting", "Role-Playing", ...),
  ...
);
foreach $options as $key => $value)
{
?>
<select name="<?php echo $key; ?>">
<?php 
  foreach ($value as $item)
  {
    // echo option for item and mark it selected if necessary
  }
?>
</select>
<?php
}
?>

你可以通过以下方式防止这种情况发生。可能重复的谢谢我会查看帖子。我已经搜索了一段时间,我猜我使用了错误的关键字。这不会让你失去所有其他参数吗?
有没有办法让查询替换已经设置的变量?
有趣的是,我会尝试一下,然后再联系你。谢谢。