Php $ _GET value is erased when setting if (! Isset)

This works great:

$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 given by something other than get, for example, '7'.

GET retrieves a value from c. If I did not put it in an if statement, it will just go into my database hundreds of times. If I put it in the if statement, it will only be entered once, but the value will be zero. I am completely at a dead end. Any help would be great.

EDIT: I think because of the answers I get, I didnโ€™t explain very well. clarify: I am using POST to send user_id, answer_body. I use GET to import latitude and longitude from Core Location (although I named this username, this is latitude). I want all of them to be in the same record, so I will associate GET with isset_POST. Also, when the GET (username) is on its own, with an INSERT request, it will do 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); 

Form Code:

 <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> 
-1
source share
2 answers

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

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

Not to mention the need to avoid the username before using in the request.

EDIT (always do 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 may be bombarded in your database. POSTs are special. Therefore, before POST processing POST make sure that REQUEST_METHOD POST .

+1
source
 if(isset($_REQUEST['submit'])) { $username= mysql_real_escape_string($_REQUEST["username"]); $query= "INSERT INTO test (latitude) VALUES ('$username')"; mysql_query($query) or die (mysql_error("error")); mysql_close(); } 

EDIT: First, I changed $ _GET and $ _POST to $ _REQUEST, so it will accept both $ _GET and $ _POST.

Secondly, I added the mysql_real_escape_string function, which avoids unwanted characters such as qutation marks to prevent SQL injection.

+1
source

Source: https://habr.com/ru/post/1337566/


All Articles