After ajax action, data is not saved in mysql database

Asked

Viewed 233 times

3

I am capturing latitude and longitude through the Geolocation plugin of Cordova, and after that I store it in two variables and then, through an ajax request, I transfer these values to a PHP script where I am saving the data in a table called map. The problem is that it executes ajax code, returns success information, nothing else in the database is saved... I can’t find the error.

PHP:

if($_GET['acao']=='btnfinaliza'){

$latitude = $_GET['tlatit'];
$longitude = $_GET['tlongt'];

$SQL = "INSERT INTO mapa (lat, lng) VALUES ('$tlati','$tlong')";

$re = mysql_query($SQL, $serve);
}

Ajax:

$('#btn_finaliza').on('click', function(){

        var Cap = function(position){
            var coord = position.coords;
            var tlatit = position.coords.latitude;
            var tlongt = position.coords.longitude;
            $tlati = $('tlatit');
            $tlong = $('tlongt');
                    $.ajax({
                            type: "get",
                            url: $server+"/conecta.php",
                            data: "latitude="+$tlati+"&longitude="+$tlong+"&acao=btn_finaliza",
                            success: function(data) {
                                intel.xdk.notification.alert('Problema cadastrado', '', 'ok'); 
                            }
                        });

        }

        });
  • 1

    do: mysql_query($SQL, $serve) or die(mysql_error()); and see if there are any errors

  • hello, returned no error...

2 answers

2

You should take php by the same names you sent by js, $_GET['latitude'] and $_GET['longitude']

data: "latitude="+$tlati+"&longitude="+$tlong+"&acao=btn_finaliza

change to:

$latitude = $_GET['latitude'];
$longitude = $_GET['longitude'];

$SQL = "INSERT INTO mapa (lat, lng) VALUES ('$tlati','$tlong')";
mysql_query($SQL, $serve) or die(mysql_error());

Do not use the mysql_* functions already removed in php7 prefer Mysqli or PDO.

Recommended reading:

Why should we not use mysql type functions_*?

Mysqli vs PDO - which is the most recommended to use?

How to prevent SQL code injection into my PHP code

Why parameterized SQL queries (name = ?) prevent SQL Injection?

  • Hello, thanks for the advice! I will adapt! I changed the code, but it is still not saving. The function returns success, that is, the message 'Registered problem' is appearing, however, is not saving yet...

  • @G.Vilela you looked in the bank to be sure?

  • Yes, it doesn’t save anything...

  • @G.Vilela, make a echo $SQL see what returns

  • console error appeared: Blocked cross-origin request

  • @G.Vilela, see that question you can test the logic of php, just passing the values in the url if you want

Show 1 more comment

0

After specifying the syntax of the code, I discovered the error... Missing an _ in php code. Was

if($_GET['acao']=='btnfinaliza')

more should be:

if($_GET['acao']=='btn_finaliza')

After the fix, the code worked and the data was saved to the database as expected. I thank you for your help and I will follow the recommendations!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.