jeudi 22 octobre 2015

Updating MySQL with JSON object using For Each -- Checkboxes

I am attempting to update the table (auth_users) with these checkbox values using QueryBuilder and the following code but it is not working for the checkboxes on my form.

I think its not working maybe because $key,"'".$value."'" does not equal the DB field name and the correct value, but actually equals "name":"checked" instead of "mb":"1"

I may be wrong on this. I basically and trying to figure out how to loop through this JSON object and update the values in a row for a particular $id. If the box is checked it would update "1" if not then it would update "0".

I have 26 checkbox values to update for each record in the DB. DB Table Structure uName,uFname,uLName,uSecurityLevel,mb,mc,ms,mi,ml,mp,mo,mst,si,ei,ai,ci,ali,hli .... etc ... (See data in JSON object below)

JQUERY FUNCTION:

function getCheckBoxRights()
  {
   var cbr = new Array();
   $(".rchk").each(function(i,e)
     {
     // loop through each checkbox and discover if it is checked,
     // and if so value = 1 else value = 0
     if($(e)[0].checked) cbr.push({"name":$(e).attr('name'),"checked":"1"});
     else cbr.push({"name":$(e).attr('name'),"checked":"0"});
     });
  console.log(cbr);
  return JSON.stringify(cbr);
  }

This returns the following JSON object for the status of the checkboxes:

[{"name":"ai","checked":"1"},{"name":"hi","checked":"1"},
{"name":"hhi","checked":"1"},{"name":"pii","checked":"1"},
{"name":"li","checked":"1"},{"name":"gi","checked":"1"},
{"name":"si","checked":"1"},{"name":"ci","checked":"1"},
{"name":"ei","checked":"1"},{"name":"voi","checked":"0"},
{"name":"hli","checked":"1"},{"name":"gli","checked":"1"},
{"name":"sli","checked":"1"},{"name":"usli","checked":"1"},
{"name":"ali","checked":"1"},{"name":"cli","checked":"1"},
{"name":"mb","checked":"1"},{"name":"mc","checked":"0"},
{"name":"md","checked":"0"},{"name":"mi","checked":"1"},
{"name":"ml","checked":"0"},{"name":"mp","checked":"0"},
{"name":"mo","checked":"0"},{"name":"ms","checked":"0"},
{"name":"mst","checked":"0"},{"name":"mu","checked":"0"}]

AJAX CALL:

$.ajax(
  {
   dataType:'JSON',
   type:'POST',
   cache:false,
   url:'dump.php',
   data:'myaction=updatec&t=auth_users&id='+data.uID+'&jsonc='+getCheckBoxRights()
  });

  var jsonc = getCheckBoxRights();
  console.log('!!!!!!!!!!!!');
  console.log('CheckBox JSON Object:'+jsonc);
  console.log('!!!!!!!!!!!!');
  console.log('ID of record to update: '+data.uID);

DUMP.PHP FILE:

case "updatec":
  {
  if(!isset($id)) break;
  $jsonc = urldecode($jsonc);
  $jsonc = stripslashes($jsonc);
  updatec($jsonc,$t,$id);
  break;
  }

function updatec($jsonc,$table,$id)
  {
  $tables = array("auth_users");
  if(!in_array($table,$tables)) return false;
  $updateQuery = new UpdateQuery($table);
  $datac = json_decode($jsonc);
    foreach($datac as $key=>$value)
    {
    $updateQuery->addItem($key,"'".$value."'");
    }
    $updateQuery->addCondition("uID",$id);
    $query = $updateQuery->generateQuery();
    $bool = TableFunctions::executeQuery($query); 
    $p = new stdClass();
    $p->ok = 0;
    $p->uID = $id; // the id of the record we updated
    if($bool) $p->ok = 1;
    echo json_encode($p);
  }

I was thinking maybe I could decode the data like this maybe and perform the update then?

$updatechk = json_decode($jsonc, true);
foreach($updatechk['name'] as $key => $updatechk['checked'] as $value)
{
  if($value) 
  {
  //how to use json array to insert data in Database
  mysql_query("UPDATE auth_users SET ".$key." = '".$value."' WHERE uID = '". $id ."'");
  }
}

Does this look like the correct way to do this? I am just a little lost trying to make this happen any assistance would be great.




Aucun commentaire:

Enregistrer un commentaire