我先应用表单获取了6个变量值用于查询,用POST传递到查询结果页面,因为查询的内容多,采用分页。点击分页页码的同时,我用GET方式传递6个变量和页码,在获取页面总GET取值正常,但是mysql查询失败。请问是不是GET与POST取值后的引用有差别?为什么第二次的查询失败?代码如下:
POST传递初次查询:
include("conn/conn.php");
$page=$_GET[page];
$content_1=$_POST[content_1]; 
$content_2=$_POST[content_2]; 
$content_3=$_POST[content_3];
$select_1=$_POST[select_1]; 
$select_2=$_POST[select_2]; 
$sql=mysql_query("SELECT count( * ) AS total FROM `yl_ppp`  where  (
P1 like '%$content_1%' and P2 like '%$content_2%' and P3 like '%$content_3%') and S1 like '$select_1') and S2  like '%$select_2%' ) " ,$conn);
//查询成功。GET传递的分页查询显示:
include("conn/conn.php");
$page=$_GET[page];
$content_1=$_GET[content_1]; echo $content_1;//此处页面显示变量传递正常
$content_2=$_GET[content_2]; 
$content_3=$_GET[content_3];
$select_1=$_GET[select_1]; 
$select_2=$_GET[select_2]; 
$sql=mysql_query("SELECT count( * ) AS total FROM `yl_ppp`  where  (
P1 like '%$content_1%' and P2 like '%$content_2%' and P3 like '%$content_3%') and S1 like '$select_1') and S2  like '%$select_2%' ) " ,$conn);
//查询失败。

解决方案 »

  1.   

    对比
    print_r($_POST) 和 print_r($_GET) 的结果
      

  2.   

    $_POST[content_1];  
    $_GET[content_2];
    csdn上我见过很多人都曾经这么写过。先不管其他的对不对 你先把php基础好好看看,起码这种情况是要避免的。$_POST['content_1'];   
    $_GET['ontent_2'];
    $_POST["content_1"];   
    $_GET["ontent_2"];单引号双引号都可以 但不能都不用。
      

  3.   

    $sql=mysql_query("SELECT count( * ) AS total FROM `yl_ppp` where (
    P1 like '%$content_1%' and P2 like '%$content_2%' and P3 like '%$content_3%') and S1 like '$select_1') and S2 like '%$select_2%' ) " ,$conn);你看一下这个给对了没??你的() 怎么不匹配啊??
    $sql=mysql_query("SELECT count( * ) AS total FROM `yl_ppp` where (a and b and c) and d) and f) " ,$conn);
    什么情况??
      

  4.   

    1,试过$_REQUEST,也是查询失败,第一个查询成功。2,括号不配是我在本帖上输入时的错误,本来代码是配对,并且第一个查询是正确输出的。3,$_GET[]和$_POST[]里面的单引号、双引号都试过的,对结果没有影响,我也在mysql_query里面尝试单引号、双引号、以及'".$content_1."'这种格式,都不行。4,变量传递是成功的,就是在mysql查询中出的问题,但无法看出问题在哪里?
      

  5.   

    $sql=mysql_query("SELECT count( * ) AS total FROM `yl_ppp` where (
    P1 like '%$content_1%' and P2 like '%$content_2%' and P3 like '%$content_3%') and S1 like '$select_1') and S2 like '%$select_2%' ) " ,$conn) or die(mysql_error());
    贴出结果
      

  6.   


    没有显示错误信息,执行了查询语句,只是没有查询到配对的信息。我用 echo '$content_1';输出发现是$content_1,我怀疑在mysql查询中变量值也变成了$content_1。
      

  7.   

    只是条件不对了?
    贴出 print_r($_GET) 的结果
    最好还贴出 print_r($_POST) 的结果
      

  8.   

    你把print_r($_GET) print_r($_POST) 打出来看看 是不是你想要的搜索条件。
      

  9.   

    第一个查询中的 print_r($_POST)输出:Array ( [content_1] => the [content_2] => [content_3] => [select_1] => [select_2] => [Submit2] => 搜索 ) 第二个查询中的print_r($_GET)输出:Array ( [content_1] => the [content_2] => [content_3] => [select_1] => [select_2] => [puid] => [page] => 20 ) 
      

  10.   

    输入的关键词为 the, 赋予$content_1变量,结果显示两次传递正常。[Submit2] => 搜索 这个有影响吗?自动产生的?
      

  11.   

    我修改了搜索前的表单提交方式,由POST改为GET,搜索结果页面代码改为:
    include("conn/conn.php");
    $page=$_GET["page"];
    $content_1=$_GET["content_1"]; 
    $content_2=$_GET["content_2"]; 
    $content_3=$_GET["content_3"];
    $select_1=$_GET["select_1"]; 
    $select_2=$_GET["select_2"]; 
    $sql=mysql_query("SELECT count( * ) AS total FROM `yl_ppp` where (
    P1 like '%$content_1%' and P2 like '%$content_2%' and P3 like '%$content_3%' and S1 like '$select_1' and S2 like '%$select_2%' ) " ,$conn);
    ...
    ...
    //搜索结果多页时,页面跳转部分代码:
    <a href="abc.php?content_1=<?php echo $content_1; ?> & content_2=<?php echo $content_2; ?> & content_3= <?php echo $content_3; ?> & select_1= <?php echo $select_1; ?> & select_2=<?php echo $select_2; ?>& page=<?php echo $i;?>"><?php echo $i;?></a>提交表单,搜索结果正常(搜索到项目190个,提示十几页的分页),点击页码跳转,搜索结果页面时出现错误,提示没有搜索到结果。
    例如文本框content_1中输入the,搜索提交后,显示190个结果,分多页显示,点击页码,搜索提示无结果。
    两次使用 print_r($_GET):
    第一次(提交表单后的结果):Array ( [content_title] => the [content_author] => [content_journal] => [select_type] => [select_species] => [Submit2] => 搜索 ) 第二次(点击跳转页面):Array ( [content_title] => the [content_author] => [content_journal] => [select_type] => [select_species] => [page] => 8 ) 从上面结果看变量值传递正常,就在mysql查询中出问题了。大家帮帮忙。
      

  12.   

    我觉得比较规范的:
    $_GET[''];
    $_GET[];
    一般不会出现什么情况。
      

  13.   

    可能是分页的问题。我刚也遇到显示第一页,按下一页就没有数据了。你刷新页面后,表单输入的内容还在表单里吗? 没有的话可能是下面这种情况?好像是按下一页后,刷新页面,$_POST[],$_GET[]里面的数据没有被保存起来。我刚在网上搜索怎么保存表单的内容,还没有解决,希望谁知道告诉我下,我是新手!
      

  14.   

    这个问题是你表单传递值类型是POST还是GET,如果你表单设置的是POST就用POST获取,如果是URL传值(明文)就用GET
    GET传值如同index.php?act=123
    就需要用GET获取act的值
      

  15.   

    应该不会有什么问题的。如果你2个都用,可以使用request的。
      

  16.   

    $_REQUEST这个怎么现在还有好多人用?这个很容易造成问题。
      

  17.   

    你说的应该是这种情况:http://www.angryfrog.com/test.php?$test=1
    ------------------------------------------------
    <?php
    echo $_GET['$test'];
    ?>
    此时如果你将单引换成双引肯定报warnning了。但一般没人闲的蛋疼在参数命名时加"$"符号。
    无论单引还是双引,都比不加引号强。效率上,单引最快,双引其次,不加引号最慢。因为php在解释时要去扫描常量符号表。
      

  18.   

    不会有区别的。楼主可以看看输出的sql语句。这样就明白些。