0

Im creating and mobile app with angularjs and php of recipes. Im trying to send the id from the recipe from the url using the $stateParams of angular to the server with http POST method. The Id is send it or received wrong because when I check the data in my localhost shows me an empty object. Anyone knows what error Im committing?

Angularjs:

-Controller for the view:

.controller('RecipeDetailCtrl', function($scope, $stateParams, Recipes) {

  Recipes.getRecipe($stateParams.recipeId).success(function(data){

        $scope.recipe = data;
        console.log($scope.recipe);
    })                                                
})

-Function in services.js:

   getRecipe : function(Id) {
        return $http({
            url: 'http://localhost/degusto/detail.php',
            method: 'POST',
            data: {'Id': Id},
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
     }) 

  }

PHP:

   require_once("connect.php");

   $user_id = $_POST['Id'];

   echo "user_id:"+$user_id;

   $result = $conn->query("SELECT * FROM Recipes WHERE Id ='".$var."'");

   $outp = "[";
    while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
       if ($outp != "[") {$outp .= ",";}
       $outp .= '{"Id":"'  . $rs["Id"] . '",';
       $outp .= '"Name":"'   . $rs["Name"]        . '",';
       $outp .= '"Date":"'   . $rs["Date"]        . '",';
       $outp .= '"Descrip":"'   . $rs["Descrip"]        . '",';
       $outp .= '"Image":"'. $rs["Image"]     . '"}';

      }
  $outp .="]";

  $conn->close();

  echo($outp);

  ?>

2 Answers2

1

You set $user_id to the id but you use $var in your select statement

$result = $conn->query("SELECT * FROM Recipes WHERE Id ='".$var."'");

should be

$result = $conn->query("SELECT * FROM Recipes WHERE Id ='".$user_id."'");

Also be aware of SQL Injection see here

Community
  • 1
  • 1
Martin
  • 2,411
  • 11
  • 28
  • 30
0

Is Id making across to PHP? I've experienced in the past that the object you want to send must be in a container object.

you could try data: $.param({'data' : {'Id': Id}}),

and then in php $data = $_POST['data'];

Dominic Scanlan
  • 1,009
  • 1
  • 6
  • 12