Doubt about query with parameters using Angularjs and http request get method

Asked

Viewed 118 times

0

first of all, good afternoon. I have come to ask for your help because I have already spent a few hours looking for a solution and nothing.

I am trying to limit the SELECT from financial list to the value set in the select field within my form (HTML), for example "SELECT * FROM financial list LIMIT $search" Doing so:

include("../sqlConnection/connection.php");
$data = json_decode(file_get_contents("php://input"));
$search = $data->fRegistro;
$sql = "SELECT * FROM listafinanceira LIMIT $search";
$stmt = $PDO->prepare( $sql );
$stmt->bindParam(1, $search , PDO::PARAM_INT);
$stmt->execute();

$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);

echo json_encode($result);

but is returning the error stating that fRegister is not an object.

That is, I’m not able to "collect" the value of select through the JSON value, if I match $search = 10, for example, it works normally. I don’t know if I was clear enough... I’m using PDO to make SQL Injection impossible through Prepared statements. http.get inside the java script to return the query is like this:

$scope.displayData = function()
    {
        $http.get("../sqlFunctions/selectForm.php",
            {'fRegistro':$scope.fRegistro}

            ).then(function(response){
            $scope.entradas = response.data;
        });
    }

I have tried to change the http request get method within javascript without success, thus:

$scope.displayData = function()
    {
        $http.get("../sqlFunctions/selectForm.php", {
            params: { 
                'registro': $scope.fRegistro
            }
            }).then(function(response){
            $scope.entradas = response.data;
        });
    }

in html:

<select name="tipo" ng-model="fRegistro" ng-init="fRegistro='10'" ng-class="['uk-select','uk-form-width-small','uk-form-small']">
                    <option value="10">10</option>
                    <option>25</option>
                    <option>50</option>
                    <option>100</option>        
</select>

1 answer

0


The problem is that the http get method by default does not accept a body and the method $http.get angular does not receive an object with parameters

Send the parameters via url, for example:

$http.get(`../sqlFunctions/selectForm.php?registro=${$scope.fRegistro}`}).then( ... );

In php you should also fix some things:

include("../sqlConnection/connection.php");

$search = $_GET["fRegistro"]; //Pega os dados passados por get atrvés da superglobal $_GET

$sql = "SELECT * FROM listafinanceira LIMIT :search"; //Use : e não $

$stmt = $PDO->prepare($sql);
$stmt->bindParam(':search', $search , PDO::PARAM_INT); //Passe o nome que deu acima no SQL, no caso ':search'

$stmt->execute();
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);

echo json_encode($result);
  • I had found a solution but it was not as complete as yours. I marked your response as positive, thanks for the collaboration!

Browser other questions tagged

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