How do I properly use AngularJS $http.post to update MySQLi table through PHP?

Viewed 175

I want to update a MySQLi table achievements through PHP. The app is coded in AngularJS and measures various statistics. When a goal is met, I send the information to a PHP script using AngularJS's $http.post. PHP should then handle the information and update my table accordingly. The $http.post returns a success message, but the table is not updated. I am confident the database connection info is correct.

My AngularJS $scope.updatePhp function using $http.post:

$scope.updatePhp = function(table, column, value, whereColumn, whereValue) {
    $http.post(
        "update-data.php", {
            'table': table,
            'column': column,
            'value': value,
            'whereColumn': whereColumn,
            'whereValue': whereValue
        }
    );
}

My AngularJS $scope.updatePhp function without using the .post shortcut:

$scope.updatePhp = function(table, column, value, whereColumn, whereValue) {

    console.log("Updating. Table: " + table + ", column: " + column + ", value: " + value + ", where: " + whereColumn + ", whereValue: " + whereValue);

    $http({
        method: 'POST',
        url: 'update-data.php',
        data: { table: table.toString(), column: column.toString(), value: value.toString(), whereColumn: whereColumn.toString(), whereValue: whereValue.toString() },
        headers: { 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8' } 
    }).then(function successCallback(response) {
        console.log("Success.");
    }, function errorCallback(response) {
        console.log("Error.");
    });

}

My entire PHP file 'update-data.php`:

<?php

    //CORS header stuff
    header("Access-Control-Allow-Origin: *");

    //PHP posted info
    $info = file_get_contents('php://input');

    //Update the table
    $hostname_DB = "databaseHost";
    $database_DB = "databaseName";
    $username_DB = "databaseUser";
    $password_DB = "databasePass";
    $portnum_DB = "databasePort";
    $mysqli = mysqli_connect($hostname_DB, $username_DB, $password_DB, $database_DB, $portnum_DB ) or die(mysqli_error());

    $table = $info->table;
    $column = $info->column;
    $value = $info->value;
    $whereColumn = $info->whereColumn;
    $whereValue = $info->whereValue;

    $query = "UPDATE '$table' SET '$column' = '$value' WHERE '$whereColumn' = '$whereValue' ";
    $mysqli->query($query) or die(mysqli_error());  

?>

I am using the PHP error_log and I am getting this error for each variable table, column, value, etc.:

PHP Notice: Trying to get property 'table' of non-object in /update-data.php on line 16.

There is definitely something wrong with the way the info is being posted or with how the posted info is being retrieved, not with the SQL.

Thank you in advance for any help you may be able to provide in sorting out this situation!

3 Answers

Probably the error is in $.param().

        data: $.param({ table: table.toString(), column: column.toString(), value: value.toString(), whereColumn: whereColumn.toString(), whereValue: whereValue.toString() })

$.param() is a JQuery function.

Check this : AngularJSParamSerializer

Try without json encode/decode to see if it works.

EDIT (comment by answerer): "In your php try to echo file_get_contents('php://input') ; And in javascript in the callback of the request console.log(response); To see if some data is successfully sent."

Try not using $.param() for data. Yo can send the data object directly.

$scope.updatePhp = function(table, column, value, whereColumn, whereValue) {

console.log("Updating. Table: " + table + ", column: " + column + ", value: " + value + ", where: " + whereColumn + ", whereValue: " + whereValue);

$http({
    method: 'POST',
    url: 'update-data.php',
    data: { 
        table: table.toString(),
        column: column.toString(),
        value: value.toString(),
        hereColumn: whereColumn.toString(),
        whereValue: whereValue.toString() 
    },
    headers: { 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8' } 
}).then(function successCallback(response) {
    console.log("Success.");
}, function errorCallback(response) {
    console.log("Error.");
});

}

The issue was with the way the data came formatted. Here is my $info variable printed:

{"table":"achievements","column":"Goal","value":1,"whereColumn":"id","whereValue":"1"}

The string from file_get_contents('php://input') was encased in curly-brackets, and comma-separated each value, both of which I had to remove.

I also had to get rid of the quotes for each string key-value pair, and since all I wanted was the value, I had to get rid of everything from the : and before (the 'key' if you will).

Here is my (albeit hacky) result, which worked excellently:

//PHP posted info
    $info = file_get_contents('php://input');
    $dataArr = explode(',',$data);

    $dCount = 0;
    foreach ($dataArr as $datum) {
        $newDatum = substr($datum, strpos($datum, ":") + 1);
        $newDatum = str_replace(array('}', '{', '"'), '', $newDatum);
        $dataArr[$dCount] = $newDatum;
        $dCount++;
    }

    $table = $dataArr[0];
    $column = $dataArr[1];
    $value = $dataArr[2];
    $whereColumn = $dataArr[3];
    $whereValue = $dataArr[4];
Related