当放置在if(!isset)时,$ _GET值被删除 [英] php $_GET value erased when placed in if(!isset)

查看:167
本文介绍了当放置在if(!isset)时,$ _GET值被删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这工作正常:

  $ username = $ _GET [username]; 
$ query =INSERT INTO test(latitude)VALUES('$ username');
mysql_query($ query)或死(mysql_error(error));
mysql_close();

这不起作用:

<$ p $如果(!isset($ _ POST ['submit']))
{
}
else
{
$ username = $ _GET [ 用户名];
$ query =INSERT INTO test(latitude)VALUES('$ username');
mysql_query($ query)或死(mysql_error(error));
mysql_close();

$ / code $ / pre
$ b $ p如果$ username被设置为除get之外的其他值,比如'7 。


GET从目标c中检索一个值。如果我没有把它放在一个if语句中,它会自动进入我的数据库数百次。如果我把它放在'if'语句中,它只会输入一次,但是值将为零。我完全被难住了。任何帮助将是伟大的。



编辑:
我想因为我得到的答案,我没有解释得很好。澄清:我使用POST来发布user_id,answer_body。我使用GET作为从Core Location导入纬度和经度的一种方式(即使我将它命名为用户名,它是纬度)。我希望他们都进入相同的条目,这就是为什么要将GET连接到isset_POST。此外,当GET(用户名)独自使用INSERT查询时,它会在数据库中创建数百个重复项。这里是Objective-C脚本:

pre $ //将任何用户名设置为数据库
float username = location.coordinate.latitude;
NSString * theValue = [NSString stringWithFormat:@%f,username];
NSString * hostStr = [NSString stringWithFormat:@http://mysite.com/page.php?username=%@,theValue];
NSData * dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:hostStr]];
NSString * serverOutput = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
NSLog(@服务器输出:%@,serverOutput);

表单代码为:

 < form action =gpstestertwo.phpmethod =post>< center> 
$< input type =textname =answer_bodymaxlentgh =5style =height:3em;/>
< input type =submitvalue =submitname =submitstyle =height:2em; width:5em; font-size:19px
/>
< / center>
< / form>


解决方案

$ _ POST ['submit '] $ _ GET ['username'] ...为什么不使用 $ _ POST ['username']



您如何发送 FORM 数据, POST GET ?或者使用 $ _ REQUEST ['username'] 来确定。

在使用查询前转义用户名


$ b 编辑(总是在 POSTs < code $,而不是 GETs ):

  if(! strcasecmp($ _ SERVER ['REQUEST_METHOD'],'POST')){
//发布操作,在这里工作只有
if(!empty($ _ POST ['username'])){
// ...在这里做最坏的...
}
}

GETs 是默认请求,您可能最终在数据库中被轰炸。 POSTs 是特殊的。因此,在做 POST 处理之前,确保 REQUEST_METHOD POST


This works fine:

$username= $_GET["username"];
$query= "INSERT INTO test (latitude) VALUES ('$username')";
mysql_query($query) or die (mysql_error("error"));
mysql_close();

This does not work:

if(!isset($_POST['submit']))
{
}
else
{
$username= $_GET["username"];
$query= "INSERT INTO test (latitude) VALUES ('$username')";
mysql_query($query) or die (mysql_error("error"));
mysql_close();
}

It works if $username is set to something other than get, like '7'.

The GET retrieving a value from objective c. If I don't put it in an if statement it will just enter itself into my database hundreds of times. If I put it in the 'if' statement it will only enter once, but the value will be zero. I'm totally stumped. Any help would be great.

EDIT: I think because of the answers I'm getting that i didn't explain well. to clarify: I am using POST to post user_id, answer_body. I am using GET as a way to import the latitude and longitude from Core Location (even though I named it username, it is the latitude). I want them all to go into the same entry, which is why im tying GET to isset_POST. Further, when GET(username) is on its own, with an INSERT query, it will make hundreds of duplicates in my database. Here is the objective-C script:

//sends whatever username is set to, to database
float username = location.coordinate.latitude;
NSString *theValue = [NSString stringWithFormat:@"%f", username];
NSString *hostStr =[NSString stringWithFormat:@"http://mysite.com/page.php?username=%@", theValue];
NSData *dataURL =  [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSUTF8StringEncoding];
NSLog(@"Server output: %@", serverOutput);

The form code is:

<form action="gpstestertwo.php" method="post"><center>
                $<input type="text" name="answer_body" maxlentgh= "5"  style="height:3em;"/>
                <input type="submit" value="submit" name="submit" style="height:2em; width:5em; font-size:19px " 
                 />
                 </center>
            </form>

解决方案

$_POST['submit'] and $_GET['username']... why not use $_POST['username'].

How are you sending the FORM data, POST or GET? Or use $_REQUEST['username'] to be sure.

Not to mention the need to escape the username before using in query.

EDIT (always do the processing on POSTs, not GETs):

if(!strcasecmp($_SERVER['REQUEST_METHOD'], 'POST')){
    // post operation, do your work HERE only
    if(!empty($_POST['username'])){
        // ... do your worst here ...
    }
}

GETs are the default request and you might end up bombarded in your database. POSTs are the special ones. So make sure the REQUEST_METHOD is POST before doing POST handling.

这篇关于当放置在if(!isset)时,$ _GET值被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆