Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带foreach的循环插入_Javascript_Php_Html - Fatal编程技术网

Javascript 带foreach的循环插入

Javascript 带foreach的循环插入,javascript,php,html,Javascript,Php,Html,我想插入到多行数据库表中 HTML: Php: 但是您没有插入,因为您没有收到html值 我做console.log(dataajax)返回此值,但不以数组形式返回: {Id:“11”,Notas:,EstadoFinal:“8”} 但你应该退回这个: {Id:“8”,注释:“”,EstadoFinal:“8”}{Id:“9”,注释:“”, EstadoFinal:“8”{Id:“10”,Notas:,EstadoFinal:“8”}{Id:“11”, 备注:,EstadoFinal:“8”}

我想插入到多行数据库表中

HTML:

Php:

但是您没有插入,因为您没有收到html值

我做
console.log(dataajax)返回此值,但不以数组形式返回:

{Id:“11”,Notas:,EstadoFinal:“8”}

但你应该退回这个:

{Id:“8”,注释:“”,EstadoFinal:“8”}{Id:“9”,注释:“”, EstadoFinal:“8”{Id:“10”,Notas:,EstadoFinal:“8”}{Id:“11”, 备注:,EstadoFinal:“8”}

print\r($\u POST);返回值:

Array
(
    [Id] => Array
        (
            [0] => 11
            [1] => 10
            [2] => 12
            [3] => 9
            [4] => 2
            [5] => 4
            [6] => 1
            [7] => 3
            [8] => 7
            [9] => 6
            [10] => 8
            [11] => 5
        )

    [Notas] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
        )

    [EstadoFinal] => Array
        (
            [0] => 8
            [1] => 8
            [2] => 8
            [3] => 8
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
        )

)

您应该始终从在页面开头创建数组对象开始:

var data_array = [];
然后在循环中,您可以执行以下操作:

$('table tr').each(function (i, row) {
    // reference all the stuff you need first
    var $row = $(row),
        $teste = $row.find("input[name^='teste']"),
        $notas = $row.find("textarea[name^='Notas']"),
        $estadofinal= $row.find("select[name^='EstadoFinal']");
    var dadosajax = {
      'Id' : $teste.val(),
      'Notas' : $notas.val(),
      'EstadoFinal' : $estadofinal.val()
    };
    data_array.push(dadosajax);
)};
这就是您获取此对象的方式:

[{Id: "8", Notas: "", EstadoFinal: "8"}, {Id: "9", Notas: "", EstadoFinal: "8"}, {Id: "10", Notas: "", EstadoFinal: "8"}, {Id: "11", Notas: "", EstadoFinal: "8"}]

您应该始终从在页面开头创建数组对象开始:

var data_array = [];
然后在循环中,您可以执行以下操作:

$('table tr').each(function (i, row) {
    // reference all the stuff you need first
    var $row = $(row),
        $teste = $row.find("input[name^='teste']"),
        $notas = $row.find("textarea[name^='Notas']"),
        $estadofinal= $row.find("select[name^='EstadoFinal']");
    var dadosajax = {
      'Id' : $teste.val(),
      'Notas' : $notas.val(),
      'EstadoFinal' : $estadofinal.val()
    };
    data_array.push(dadosajax);
)};
这就是您获取此对象的方式:

[{Id: "8", Notas: "", EstadoFinal: "8"}, {Id: "9", Notas: "", EstadoFinal: "8"}, {Id: "10", Notas: "", EstadoFinal: "8"}, {Id: "11", Notas: "", EstadoFinal: "8"}]

可能问题是$stmt->bind_param,因为$Notas、$estadofail、$Colaborador1、$value是字符串而不是数组

$stmt->bind_param("ssss", $Notas[$value], $EstadoFinal[$value], $Colaborador1[$value], $value ); // ou ("si", $hash, $idUsuario)
此外,ajax调用只向php文件发送四个dinstinc变量,而不是多维数组,因此必须迭代要插入DB中的所有字段

例如:

var dadosajax = []

$('table tr').each(function(){
    var tmpdata = {
     'Id' : $(this).find("input[name='teste']").val(),
     'Notas' : $(this).find("textarea[name='Notas']").val(),
     'EstadoFinal' : $(this).find("select[name='EstadoFinal']").val()
    };

dadosajax.push(tmpdata );

});

console.log(dadosajax);
    $.ajax({
        url: './resolucaooratorio',
        type: 'POST',
        cache: false,
        data: dadosajax,
        error: function(){
          $(".error_message").removeClass('hide');
        },
        success: function(result)
        { 
        console.log(result);
         $("#spoiler8").load(" #spoiler8 > *");
        }       
    });

可能问题是$stmt->bind_param,因为$Notas、$estadofail、$Colaborador1、$value是字符串而不是数组

$stmt->bind_param("ssss", $Notas[$value], $EstadoFinal[$value], $Colaborador1[$value], $value ); // ou ("si", $hash, $idUsuario)
此外,ajax调用只向php文件发送四个dinstinc变量,而不是多维数组,因此必须迭代要插入DB中的所有字段

例如:

var dadosajax = []

$('table tr').each(function(){
    var tmpdata = {
     'Id' : $(this).find("input[name='teste']").val(),
     'Notas' : $(this).find("textarea[name='Notas']").val(),
     'EstadoFinal' : $(this).find("select[name='EstadoFinal']").val()
    };

dadosajax.push(tmpdata );

});

console.log(dadosajax);
    $.ajax({
        url: './resolucaooratorio',
        type: 'POST',
        cache: false,
        data: dadosajax,
        error: function(){
          $(".error_message").removeClass('hide');
        },
        success: function(result)
        { 
        console.log(result);
         $("#spoiler8").load(" #spoiler8 > *");
        }       
    });

您需要为每一行获取每个元素的值。你只得到第一个值。也许这是可行的,未经测试:

function inserir_registo8()
{  
    var Ids = [];
    $("input[name^='teste']").each(function() {Ids.push(this.value)});

    var Notass = [];
    $("textarea[name^='Notas']").each(function() {Notass.push($(this).val())});

    var EstadoFinals = [];
    $("select[name^='EstadoFinal']").each(function() {EstadoFinals.push($(this).val())});

    var dadosajax = {
     'Id[]' : Ids,
     'Notas[]' : Notass,
     'EstadoFinal[]' : EstadoFinals
    };
    console.log(dadosajax);
    $.ajax({
        url: './resolucaooratorio',
        type: 'POST',
        cache: false,
        data: dadosajax,
        error: function(){
          $(".error_message").removeClass('hide');
        },
        success: function(result)
        { 
        console.log(result);
         $("#spoiler8").load(" #spoiler8 > *");
        }       
    });

}
如果你用
print\r($\u POST)向我展示,我也可以在PHP方面提供帮助$POST中的内容

您可以通过使用索引迭代所有数组来处理$\u POST中的数组:

$Ids = $_POST["Id"];      
$Notass = $_POST["Notas"];
$EstadoFinals = $_POST["EstadoFinal"];
$Colaborador1 = $_SESSION['usuarioId'];   
$count = count($Ids);
for($i = 0; $i<$count; ++$i) {                  
    $value = $Ids[$i];
    $Notas = $Notass[$i];
    $EstadoFinal = $EstadoFinals[$i];
    if(!empty($EstadoFinal)) {
        $query = 'UPDATE RegistoOratorio SET Notas= ?, EstadoFinal= ?, Colaborador1= ? WHERE Id = ? '; // ou WHERE id = ?
        $stmt = $conn->prepare( $query );
        $stmt->bind_param("ssss", $Notas, $EstadoFinal, $Colaborador1, $value ); // ou ("si", $hash, $idUsuario)
        $stmt->execute();   
    }
} 
$Ids=$\u POST[“Id”];
$Notass=$_POST[“Notas”];
$EstadoFinal=$_POST[“EstadoFinal”];
$Colaborador1=$\u会话['usuarioId'];
$count=计数($id);
对于($i=0;$iprepare($query);
$stmt->bind_param($sss“,$Notas,$EstadoFinal,$Colaborador1,$value);//ou($si“,$hash,$idUsuario)
$stmt->execute();
}
} 

我添加了一个检查estadoFinal是否为空的选项。在PHP中添加比在javascript代码中添加更容易。

您需要获取每行的每个元素的值。按照目前的方式,您只获取第一个值。可能这会起作用,未经测试:

function inserir_registo8()
{  
    var Ids = [];
    $("input[name^='teste']").each(function() {Ids.push(this.value)});

    var Notass = [];
    $("textarea[name^='Notas']").each(function() {Notass.push($(this).val())});

    var EstadoFinals = [];
    $("select[name^='EstadoFinal']").each(function() {EstadoFinals.push($(this).val())});

    var dadosajax = {
     'Id[]' : Ids,
     'Notas[]' : Notass,
     'EstadoFinal[]' : EstadoFinals
    };
    console.log(dadosajax);
    $.ajax({
        url: './resolucaooratorio',
        type: 'POST',
        cache: false,
        data: dadosajax,
        error: function(){
          $(".error_message").removeClass('hide');
        },
        success: function(result)
        { 
        console.log(result);
         $("#spoiler8").load(" #spoiler8 > *");
        }       
    });

}
如果您使用
print\r($\u POST);
显示$POST中的内容,我也可以在PHP方面提供帮助

您可以通过使用索引迭代所有数组来处理$\u POST中的数组:

$Ids = $_POST["Id"];      
$Notass = $_POST["Notas"];
$EstadoFinals = $_POST["EstadoFinal"];
$Colaborador1 = $_SESSION['usuarioId'];   
$count = count($Ids);
for($i = 0; $i<$count; ++$i) {                  
    $value = $Ids[$i];
    $Notas = $Notass[$i];
    $EstadoFinal = $EstadoFinals[$i];
    if(!empty($EstadoFinal)) {
        $query = 'UPDATE RegistoOratorio SET Notas= ?, EstadoFinal= ?, Colaborador1= ? WHERE Id = ? '; // ou WHERE id = ?
        $stmt = $conn->prepare( $query );
        $stmt->bind_param("ssss", $Notas, $EstadoFinal, $Colaborador1, $value ); // ou ("si", $hash, $idUsuario)
        $stmt->execute();   
    }
} 
$Ids=$\u POST[“Id”];
$Notass=$_POST[“Notas”];
$EstadoFinal=$_POST[“EstadoFinal”];
$Colaborador1=$\u会话['usuarioId'];
$count=计数($id);
对于($i=0;$iprepare($query);
$stmt->bind_param($sss“,$Notas,$EstadoFinal,$Colaborador1,$value);//ou($si“,$hash,$idUsuario)
$stmt->execute();
}
} 


我添加了一个检查estadoFinal是否为空的选项。在PHP中添加要比在javascript代码中添加更容易。

请发布您正在填充
dataajax
@Jbadminton的部分填写dadosajax的部分是我在问题开始时发布的html,作为辅助点,因为您使用的是准备好的语句,然后是
mysqli_不需要real_escape_string
。bind_param方法将自动进行转义。事实上,如果您也使用real_escape_string,则通过双重转义数据,它实际上会产生问题。
dataajax
不会出现在代码中的任何地方。您的意思是说
dadosajax
?只使用一次prepare,然后执行在循环中。这就是为什么它被称为prepared statement。请发布您正在填充
dataajax
@Jbadminton的部分。填充dadosajax的部分是我在问题开始时发布的html作为次要点,因为您使用的是prepared statements,那么就不需要
mysqli\u real\u escape\u string
。绑定_param方法将自动进行转义。事实上,如果您也使用真实的转义字符串,它实际上会通过双重转义数据而产生问题。
dataajax
不会出现在代码中的任何地方。您的意思是
dadosajax
?只使用一次prepare,然后在循环中执行。这就是为什么它被称为prepared stat是的,需要一个数组…除了您可能应该展示如何执行循环以从每组输入中获取所有值之外。此代码仍然只在数组中生成一个对象。这只是解决方案的一半,这要好得多。jquery是否将其序列化为您期望的结构?请参阅我在主线程上的最后一条注释…有这也是另一个有待进一步解决的问题。这可能会进一步加剧,除非以“正常”的方式进行序列化(即,与
serialize()
或简单的非ajax回发相同)。是的,需要一个数组…除了您可能应该演示如何执行循环以从每组输入中获取所有值之外。此代码仍然只在数组中生成一个对象。这只是解决方案的一半,这要好得多。jquery是否将其序列化为您期望的结构?请参阅我在主线程上的最后一个注释…没有还有一个问题还在后面等待。这可能会进一步加剧,除非以“正常”方式序列化(即与
serialize()
或简单的非ajax回发方式相同)。使用什么打印($\POST)更新问题;变量var数据ajax中的returns应仅获取已填充state列的id更新问题的print\r($\upost);变量var数据ajax中的returns应仅获取已填充state列的id