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 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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'];

1 Comment

I tried using the container object, but still not working, sends me an undefined object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.