Wednesday, 30 January 2019

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

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: $.param({ 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

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

    //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());  

?>

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



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

No comments:

Post a Comment