在新记录列表中保存整个范围的(网格)记录


Save whole range of (grid) records in new records list

我试图在一个ajax请求中将整个范围(网格)记录从存储发送到服务器,并将所有记录保存在另一个表中。

然而,它似乎缺少一些细节。

在客户端,console.log (records)显示所有要发送的对象。

在客户端,回显'console.log ("PHP: ")。元数据。’")的退出;回报console.log("PHP:");

EXTJS 5

newList: function (button, e, eOpts) {
    var grid = this.lookupReference('gridRef');
    var store = grid.getStore(),
   // http://stackoverflow.com/questions/37663867/what-is-the-best-way-to-create-a-list-from-the-rows-of-a-grid
    //get whole range of records from the store
    var records = store.getRange().map(function(record) { return record.getData() }); 
    //console.log(records); // OK

    Ext.Ajax.request({
        url: 'php/newList.php?',
        method: 'POST',
        //jsonData: records,
        params:{records:Ext.encode(records)} //EDITED
        success: function(conn, response, options, eOpts) {
               console.log('Success');
        },
        failure: function(conn, response, options, eOpts) {
           console.log('Error')
        }
    });
PHP

    $records = stripslashes($_POST['records']);
    $data = json_decode($records, true);
    //var_dump($_POST);
    // echo 'console.log("PHP: '.$data.'")', exit;  

    foreach($data as $row) {
        $item[] = $row['item'];
    }

     for($i=0; $i<count($data);$i++){
         $sqlQuery = "INSERT INTO tab_list (item) VALUES (?)";
     }
    if($statement = $conexao->prepare($sqlQuery)){
        $statement->bind_param("s", $item[$i]);
        $statement->execute();
        $success= true;
    }else{
        $erro = $conexao->error;
        $success = false;
    }
    $sucess = array("success" => mysqli_errno($conexao) == 0);
    echo json_encode(array(
        "success" => $sucess
    ));
    $statement->close();
    $conexao->close();

编辑:

我更改了ajax请求jsonData: recordsparams:{records:Ext.encode(records)}

在PHP代码上使用var_dump($_POST);,我在DevTools上得到以下响应

array(10) {
  [0]=>
  array(5) {  
    ["item"]=>
    string(9) "item five"
    ["id"]=>
    string(22) "MyList.listDataModel-1"
  }
  [1]=>
  array(5) {
    ["item"]=>
    string(10) "item seven"
    ["id"]=>
    string(22) "MyList.listDataModel-2"
  }
  [2]=>
  array(5) {
    ["item"]=>
    string(9) "item four"
    ["id"]=>
    string(22) "MyList.listDataModel-3"
  }
...

但是,我仍然不能在表中插入这些数据并创建一个新的列表。问题现在在PHP代码中。

PHP解决方案:

  $records = stripslashes($_POST['records']);
  $data = json_decode($records, true);
//var_dump($_POST);
// echo 'console.log("PHP: '.$data.'")', exit;  

foreach($data as $row) {
    $item[] = $row['item'];
}

 for($i=0; $i<count($data);$i++){
     $sqlQuery = "INSERT INTO tab_list (item) VALUES (?)";  
     //within for
     if($statement = $conexao->prepare($sqlQuery)){
     $statement->bind_param("s", $item[$i]);
     $statement->execute();
     $success= true;
 }else{
    $erro = $conexao->error;
    $success = false;
    }
}
$sucess = array("success" => mysqli_errno($conexao) == 0);
echo json_encode(array(
    "success" => $sucess
));
$statement->close();
$conexao->close();

是否在foreach循环之前初始化$item ?看起来你想把if($statement = $conexao->prepare(...行放在for(或者可能是foreach)循环中。

编辑完问题后,考虑使用单个 INSERT语句作为解决方案的替代方案:

$total = count($item);
$sqlQuery = 'INSERT INTO tab_list (item) VALUES ' . rtrim(str_repeat('(?),', $total), ',');
$statement = $conexao->prepare($sqlQuery);
if ($statement)
{
    for ($i = 0; $i < $total; $i++)
    {
        $statement->bind_param('s', $item[$i]);
    }
    $statement->execute();
    $success = true;
}
else
{
    $erro = $conexao->error;
    $success = false;
}