1

I want to write select query in php to get data from database using variable from front-end. In other words I need to do something like this: SELECT email FROM users WHERE token = '$token' What I currently have, I have code which posts my variables to back end:

app.controller('customersCtrl', function($scope, $http,$templateCache) {
    $scope.subscribe = function (gameId){
        $scope.codeStatus = "";
        $http({
            url: "php/test.php",
            method: "POST",
            data: {
                userId: JSON.parse(localStorage.getItem('loggedUserInfo')),
                gameId: gameId,
                token:JSON.parse(localStorage.getItem('token'))
            },
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            cache: $templateCache
        }).success(function(response) {
            $scope.codeStatus = response.data; 
            console.log(response);
        });

    };

});

And here is my php code:

<?php
$lnk = mysql_connect('localhost', 'root', '')
    or die ('Not connected : ' . mysql_error());

mysql_select_db('pitch', $lnk) or die ('Can\'t use db : ' . mysql_error());

$data = json_decode(file_get_contents("php://input"));
$token=$data->token;   

$dump1 = mysql_query("SELECT email FROM users where token = '$token' ");

if(!$dump1) exit("Error - ".mysql_error()); 
$getList = array(); 
$outp = "";
while ($row = mysql_fetch_assoc($dump1)) { 
    $relations = array();
    $getList[] = $row;
}
$output = json_encode(array('users' => $getList));

echo $output; 
?>

The weirdest thing for me is that when I try to go to url localhost/php/test.php I get an Notice: Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/php/test.php on line 8, which means I guess that $token is empty, but when I console.log(response) in angular it shows me my token in console which I think means that data from front-end was passed to back-end. Or my understanding is wrong?

Will be really thankful for help!

1
  • The problem was solved by using cookies instead of trying to get data from local storage. Commented Oct 9, 2016 at 21:59

1 Answer 1

2

first thing is try to var_dump($_POST), or try to var_dump($data) on your test.php

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

Comments

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.