Update a MySQL database from AngularJS & PHP

I found myself working with AngularJS and needing to update my MySQL database with information generated in AngularJS. Normally, I only ever updated MySQL databases using information already accessible with my PHP code, either by initializing it within the same scope as my queries or even by passing it via POST from one PHP page to the other. This time, however, the data was generated in my AngularJS script, not PHP.

The solution still requires PHP in order to send the actual query, but the data must be sent from AngularJS to the PHP script. Here is how I managed it.

In my AngularJS code, I wrote a function to send an update to the page via built-in HTTP POST functions. Essentially, the AngularJS function takes in basic info like what to replace — and with what — and sends it as individual pieces of POST data. Take a look:

/* AngularJS: Update values in MySQL with POST (handle with a PHP program) */
$scope.updatePhp = function(table, column, value, whereColumn, whereValue) {

    $http({
        method: 'POST',
        url: 'update-data.php',
        data: { 
            'table': table,
            'column': column,
            'value': value,
            'whereColumn': whereColumn,
            'whereValue': whereValue
        },
        headers: { 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8' } 
    }).then(function successCallback(response) {
        /* Fires on success; for debugging */
    }, function errorCallback(response) {
        /* Fires on error; for debugging */
    });

}

This bundles the data up nicely if you intend to make a MySQL query with it, specifically one like “UPDATE $table SET $column = $value WHERE $whereColumn = $whereValue” — though you can easily tweak the code and query together to get the desired results.

Now that you have the data, you need to make the actual query. The data is sent via POST to a file with the URL “update-data.php” which is clearly a PHP file. Inside this file, I do the following:

  1. Get the contents of the POST data.
  2. Translate the data for the program to easily access.
  3. Make the MySQL query using the data values.

Here is the code for the pseudocode above:

<?php

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

//PHP posted info
$data = 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++;
}

//MySQL
$hostname_DB = "HOSTNAME";
$database_DB = "DATABASE";
$username_DB = "USER";
$password_DB = "PASS";
$PORT_NUM = 80; // Get the proper port number here
$mysqli = mysqli_connect($hostname_DB, $username_DB, $password_DB, $database_DB, "PORTNUM" ) or die(mysqli_error());

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

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

?>

Again, some editing may be required to fit custom query needs.

Good luck.

Leave a Reply

Your email address will not be published. Required fields are marked *