我先应用表单获取了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);
//查询失败。
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);
//查询失败。
print_r($_POST) 和 print_r($_GET) 的结果
$_GET[content_2];
csdn上我见过很多人都曾经这么写过。先不管其他的对不对 你先把php基础好好看看,起码这种情况是要避免的。$_POST['content_1'];
$_GET['ontent_2'];
$_POST["content_1"];
$_GET["ontent_2"];单引号双引号都可以 但不能都不用。
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);
什么情况??
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());
贴出结果
没有显示错误信息,执行了查询语句,只是没有查询到配对的信息。我用 echo '$content_1';输出发现是$content_1,我怀疑在mysql查询中变量值也变成了$content_1。
贴出 print_r($_GET) 的结果
最好还贴出 print_r($_POST) 的结果
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查询中出问题了。大家帮帮忙。
$_GET[''];
$_GET[];
一般不会出现什么情况。
GET传值如同index.php?act=123
就需要用GET获取act的值
------------------------------------------------
<?php
echo $_GET['$test'];
?>
此时如果你将单引换成双引肯定报warnning了。但一般没人闲的蛋疼在参数命名时加"$"符号。
无论单引还是双引,都比不加引号强。效率上,单引最快,双引其次,不加引号最慢。因为php在解释时要去扫描常量符号表。