Update database with php and json array

This php script shows how to grab ajax sent from javascript and update in data base, it handles multiple updates.


<?php

$request = json_decode($HTTP_RAW_POST_DATA, true); //Grab the json sent by javascript ajax
$action  = strtolower($request['action']); //Same here see what action was sent
$data    = $request['data']; // query if data was sent

switch($action) {
case 'update':
// open data base to update the fields
$max = sizeof($data); // see if the $data has size
// is $data array and is not empty?
if (is_array($data)  && ($max > 0)) {
for ($i = 0; $i < $max; $i++) {
//if everything is ok place here the query to update the database
//this way you are able to update multiple fieds
//commit the transaction
}
}
//close the data base
//if everything goes as espected then
if ($flag) {
$success = true;
} else {
$success = false;
}
die(json_encode(array('success':$success)));
break;
}

?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s