AdrianBorkala
AdrianBorkala

Reputation: 179

AngularJS posting data to a php mysql insert

I'm writing a webapp which successfully allows me to login to Facebook (I'm using Phonegap and the Phonegap Facebook plugin). I then want to store the logged in users name and ID. To start with as a simple test I wanted to get the following controller to run collect the ID, display it in the xcode console to confirm it was there and then send it to the php code below to then store in a mysql table. I can't seem to get it working and I think it's possibly the format of my data in the {}'s within the $http.post but it's a bit beyond my current knowledge to figure this one out. Any ideas?

function FacebookCtrl($scope) {
    FB.api('/me', function(response) {
       var fbid=response.id;
       console.log('Testing, ' + fbid + '.');
       $http.post('http://somedomain.co.uk/php/users.php', {uid: fbid})
       console.log('Complete');
    });
}

The php code at the receiving end is:

<?php
    $data = file_get_contents("php://input");
    $objData = json_decode($data);

    $uid = $objData->uid;

    try {
        include 'database.php';
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare('INSERT INTO Userdata (oauth_uid) VALUES (:userid)');
    $stmt->execute(array(
            ':userid' => $uid,
    ));
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
?>

The same php code works with another one of my controllers however the difference is that the other controller captures the data passed from a form so the http.post looks like this:

$http.post('http://somedomain.co.uk/php/submitdata.php', {Position1: $scope.position1}

And the code in the php that captures this data is:

$Position1 = $objData->Position1->Name;

As the code all works on another controller, I'm assuming that the issue is with how I'm formatting the data I'm passing between the {}'s?

Upvotes: 0

Views: 4158

Answers (1)

Mak
Mak

Reputation: 20453

Try to define success \ error callbacks

$http.post("http://somedomain.co.uk/php/users.php", {uid: fbid})
    .success(function(data, status, headers, config) {
        $scope.data = data;
    }).error(function(data, status, headers, config) {
        $scope.status = status;
    });

What will it say then?

Upvotes: 1

Related Questions