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

但是,它似乎缺少一些细节。

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

在客户端,回显“ console.log(“ PHP:”。$ data。'“)'出口;返回console.log(“ PHP:”);

扩展JS 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: records中更改了
 params:{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();

最佳答案

$item是否在该foreach循环之前初始化?看起来您希望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;
}

09-10 11:39
查看更多