使用For Each - Checkboxes使用JSON对象更新MySQL

时间:2015-10-22 17:51:45

标签: php jquery mysql json checkbox

我正在尝试使用QueryBuilder和以下代码使用这些复选框值更新表(auth_users),但它不适用于我的表单上的复选框。

我认为它不起作用可能是因为$ key,“'”。$ value。“'”不等于DB字段名称和正确的值,但实际上等于“name”:“checked”而不是“mb” : “1”

我可能错了。我基本上并试图弄清楚如何循环这个JSON对象并更新特定$ id的行中的值。如果选中此框,则会更新“1”,否则会更新“0”。

我为数据库中的每条记录更新了26个复选框值。 数据库表结构 uName,uFname,uLName,uSecurityLevel,mb,mc,ms,mi,ml,mp,mo,mst,si,ei,ai,ci,ali,hli .... etc ...(请参阅下面的JSON对象中的数据)

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);
  }

这将返回以下JSON对象以获取复选框的状态:

[{"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文件:

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);
  }

我想也许我可以解码这样的数据然后执行更新?

$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 ."'");
  }
}

这看起来是正确的方法吗?我只是有点迷失试图让这种情况发生,任何帮助都会很棒。

复选框HTML(按请求)

<div id="inventoryRights" style="display:none;float:right;width:43.3%;"><strong><u>Inventory Rights</u></strong><p>
<input type="checkbox" id="ai" name="ai" data-table="auth_users" class="rchk json-input" data-prop="ai"> Active&nbsp;&nbsp;
<input type="checkbox" id="hi" name="hi" data-table="auth_users" class="rchk json-input" data-prop="hi"> Hold&nbsp;&nbsp;
<input type="checkbox" id="hhi" name="hhi" data-table="auth_users" class="rchk json-input" data-prop="hhi"> Hold Indefinately&nbsp;&nbsp;
<input type="checkbox" name="pii" id="pii" data-table="auth_users" class="rchk json-input" data-prop="pii"> Pending&nbsp;&nbsp;
<input type="checkbox" id="li" name="li" data-table="auth_users" class="rchk json-input" data-prop="li"> Link<p>
<input type="checkbox" id="gi" name="gi" data-table="auth_users" class="rchk json-input" data-prop="gi"> Greeny&nbsp;&nbsp;
<input type="checkbox" name="si" id="si" data-table="auth_users" class="rchk json-input" data-prop="si"> Sold&nbsp;&nbsp;
<input type="checkbox" id="ci" name="ci" data-table="auth_users" class="rchk json-input" data-prop="ci"> Cancelled&nbsp;&nbsp;
<input type="checkbox" id="ei" name="ei" data-table="auth_users" class="rchk json-input" data-prop="ei"> Edit&nbsp;&nbsp;
<input type="checkbox" name="voi" data-table="auth_users" class="rchk json-input" data-prop="voi"> View Only<p>
<strong><u>Default Views</u></strong><p>
<input type="checkbox" name="hli" id="hli" data-table="auth_users" class="rchk json-input" data-prop="hli"> Hold List&nbsp;&nbsp;
<input type="checkbox" name="gli" id="gli" data-table="auth_users" class="rchk json-input" data-prop="gli"> Greeny List&nbsp;&nbsp;
<input type="checkbox" id="sli" name="sli" data-table="auth_users" class="rchk json-input" data-prop="sli"> Sold List&nbsp;&nbsp;
<input type="checkbox" id="usli" name="usli" data-table="auth_users" class="rchk json-input" data-prop="usli"> Unsold List<p>
<input type="checkbox" name="ali" id="ali" data-table="auth_users" class="rchk json-input" data-prop="ali"> Archived List&nbsp;&nbsp;
<input type="checkbox" name="cli" data-table="auth_users" class="rchk json-input" id="cli" data-prop="cli"> Cancelled List</div>

<div id="allowableRights" style="display:none;padding:5px;width:55%;">&nbsp;<strong><u>System Rights</u></strong><p>
<input type="checkbox" id="mb" name="mb" data-table="auth_users" class="rchk json-input" data-prop="mb"> Manage Bedrooms&nbsp;&nbsp;
<input type="checkbox" id="mc" name="mc" data-table="auth_users" class="rchk json-input" data-prop="mc"> Manage Companies&nbsp;&nbsp;
<input type="checkbox" name="md" data-table="auth_users" id="md" class="rchk json-input" data-prop="md"> Manage Descriptions&nbsp;&nbsp;
<input type="checkbox" name="mi" data-table="auth_users" class="rchk json-input" id="mi" data-prop="mi"> Manage Inventory<p>
<input type="checkbox" id="ml" name="ml" data-table="auth_users" class="rchk json-input" data-prop="ml"> Manage Locations&nbsp;&nbsp;
<input type="checkbox" id="mp" name="mp" data-table="auth_users" class="rchk json-input" data-prop="mp"> Manage Properties&nbsp;&nbsp;
<input type="checkbox" name="mo" id="mo" data-table="auth_users" class="rchk json-input" data-prop="mo"> Manage Owners<p>
<input type="checkbox" name="ms" data-table="auth_users" class="rchk json-input" id="ms" data-prop="ms"> Manage Sleeps&nbsp;&nbsp;
<input type="checkbox" id="mst" name="mst" data-table="auth_users" class="rchk json-input" data-prop="mst"> Manage States&nbsp;&nbsp;
<input type="checkbox" id="mu" name="mu" data-table="auth_users" class="rchk json-input" data-prop="mu"> Manage Users</div>

以下是管理员选择管理用户时的样子(所有数据都从数据库中提取并填充字段和复选框。) Manage User

1 个答案:

答案 0 :(得分:0)

我终于把它弄出来了。 =)下面显示了工作代码。问题是2部分。一个我只是循环遍历JSON对象一次,两个JSON对象的形成是不正确的。

以下是工作代码: 在DUMP.PHP

case "updatec":
{
 if(!isset($id)) break;
 updatec($jsonc,$table,$id)
 break;
}

 function updatec($jsonc,$table,$id)
 {
 $updateQuery = new UpdateQuery($table);
 $datac = json_decode($jsonc,true);
    foreach ($datac as $key => $jsons) {
      foreach($jsons as $key => $value) {
      $updateQuery->addItem($key,"'".$value."'");
      }
    }
 }

在FUNCTION Rewrite中:

function getCheckBoxRights()
{
var cbr = new Array();
$(".rchk").each(function(i,e)
   {
 //get type and info, then setup an object to push onto the array
   if($(e)[0].checked)
      {
      var datatype = $(e).attr('name'),
      info = "1",
      obj  = {};
      obj[datatype] = info;
      cbr.push(obj);
      }
      else
      {
      var datatype = $(e).attr('name'),
      info = "0",
      obj  = {};
      obj[datatype] = info;
      cbr.push(obj);
      }
   });
 console.log(cbr);
 return JSON.stringify(cbr);
}

感谢至少回复并允许我再次浏览所有代码并尝试创建一种新方法来执行此操作。 Jquery / AJAX可能很有挑战性,但学习很棒!