josei josei - 2 months ago 9
PHP Question

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

I'm trying to send the whole range of (the grid) records from the store to the server in a single ajax request and save all records in another table.

However it seems to lack some detail.

On the client side a console.log (records) displays all the objects to be sent.

On the client side a echo 'console.log ("PHP:'. $ data. '")' exit; returns 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();


EDITED:

I changed in ajax request
jsonData: records
for
params:{records:Ext.encode(records)}


With
var_dump($_POST);
on PHP code I get the following response on 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) {
["ordem"]=>
int(3)
["item"]=>
string(9) "item four"
["id"]=>
string(22) "MyList.listDataModel-3"
}
...


However, I am still not able to insert these data in the table and create a new list. The problem is now in the PHP code.

PHP solution:

$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();

Answer

Is $item initialized prior to that foreach loop? Looks like you intended the if($statement = $conexao->prepare(... line to be within the for (or possibly the foreach) loop.

After editing your question, consider using a single INSERT statement as an alternative to your solution:

$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;
}
Comments