Javascript 在新记录列表中保存整个范围的(网格)记录
我试图通过一个ajax请求将整个范围(网格)的记录从存储区发送到服务器,并将所有记录保存在另一个表中 然而,它似乎缺少一些细节 在客户端,console.log(记录)显示所有要发送的对象 在客户端有一个echo'console.log(“PHP:'.$data.”)出口;返回console.log(“PHP:”) EXTJS 5Javascript 在新记录列表中保存整个范围的(网格)记录,javascript,php,extjs,grid,extjs5,Javascript,Php,Extjs,Grid,Extjs5,我试图通过一个ajax请求将整个范围(网格)的记录从存储区发送到服务器,并将所有记录保存在另一个表中 然而,它似乎缺少一些细节 在客户端,console.log(记录)显示所有要发送的对象 在客户端有一个echo'console.log(“PHP:'.$data.”)出口;返回console.log(“PHP:”) EXTJS 5 newList: function (button, e, eOpts) { var grid = this.lookupReference('gridRe
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
但是,我仍然无法在表中插入这些数据并创建新列表。问题现在出现在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();
$records=stripslashes($_POST['records']);
$data=json_decode($records,true);
//变量转储($\u POST);
//echo'console.log(“PHP:'.$data.”),退出;
foreach($行数据){
$item[]=$row['item'];
}
对于($i=0;$iprepare($sqlQuery)){
$statement->bind_参数(“s”,$item[$i]);
$statement->execute();
$success=true;
}否则{
$erro=$conexao->错误;
$success=false;
}
}
$suces=array(“success”=>mysqli_errno($conexao)==0);
echo json_编码(数组(
“成功”=>$success
));
$statement->close();
$conexao->close();
在foreach循环之前,$item
是否已初始化?看起来您希望if($statement=$conexao->prepare(…
行位于for
(或者可能是foreach
)循环中
在编辑您的问题后,考虑使用一个单一的代码> INSERT < /C>语句作为您的解决方案的替代方案:
$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;
}
$total=计数($item);
$sqlQuery='INSERT INTO tab_列表(项)值'.rtrim(str_repeat('(?),',',$total),',');
$statement=$conexao->prepare($sqlQuery);
如果($报表)
{
对于($i=0;$i<$total;$i++)
{
$statement->bind_参数('s',$item[$i]);
}
$statement->execute();
$success=true;
}
其他的
{
$erro=$conexao->错误;
$success=false;
}
是否在foreach
循环之前初始化了$item
?看起来您希望if($statement=$conexao->prepare(…
行位于for
的内(或者可能是foreach
)循环。谢谢。让我惊讶的是,首先是console.log没有显示任何数据。应该显示console.log(“PHP:Array”);对应于通过ajax请求从客户端发送的对象。不要打印console.log
,而是尝试var\u dump($\u POST)
查看实际发送到newList.php的数据。检查控制台日志的网络选项卡中的响应。使用var_dum($\u POST)返回数组(0){}{“success”:{“success”:false}。然后没有数据发送到newList.php。$records
和$data
将为空。没有问题。愉快的编码
$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();
$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;
}