我想插入到多行数据库表中。
HTML:
<tr>
<td style="display:none"><input type="text" rows="4" name="teste[]" value="<?php echo $produto8["Id"]; ?>"></td>
<td><textarea rows="4" name="Notas[]" value="<?php echo $produto8["Notas"]; ?>"></textarea></td>
<td><select class="form-control" name="EstadoFinal[]"><?php echo $produto8["EstadoFinal"]; ?><option value=<?php echo $ln['Id']; ?>><?php echo $ln['Estado']; ?></option>
<?php
$sql = "SELECT * FROM raddb.Status WHERE Id IN ('8') ORDER BY Estado ASC";
$qr = mysqli_query($conn, $sql);
while($ln = mysqli_fetch_assoc($qr)){
echo '<option value="'.$ln['Id'].'">'.$ln['Estado'].'</option>';
}
?>
</select></td>
</tr>
资料提交:
function inserir_registo8()
{
var dadosajax = {
'Id' : $("input[name^='teste']").val(),
'Notas' : $("textarea[name^='Notas']").val(),
'EstadoFinal' : $("select[name^='EstadoFinal']").val()
};
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 > *");
}
});
}
Php:
$Id = mysqli_real_escape_string($conn, $_POST["Id"]);
$Notas = mysqli_real_escape_string($conn, $_POST["Notas"]);
$EstadoFinal = mysqli_real_escape_string($conn, $_POST["EstadoFinal"]);
$Colaborador1 = $_SESSION['usuarioId'];
foreach($Id as $value) {
$query = 'UPDATE RegistoOratorio SET Notas= ?, EstadoFinal= ?, Colaborador1= ? WHERE Id = ? '; // ou WHERE id = ?
$stmt = $conn->prepare( $query );
$stmt->bind_param("ssss", $Notas[$value], $EstadoFinal[$value], $Colaborador1[$value], $value ); // ou ("si", $hash, $idUsuario)
$stmt->execute();
}
但是您没有插入,因为您没有收到html值。
我做
console.log (dataajax);
返回此,但不返回数组:{Id:“ 11”,Notas:“”,EstadoFinal:“ 8”}
但是您应该返回以下内容:
{Id:“ 8”,Notas:“”,EstadoFinal:“ 8”} {Id:“ 9”,Notas:“”,
EstadoFinal:“ 8”} {Id:“ 10”,Notas:“”,EstadoFinal:“ 8”} {Id:“ 11”,
Notas:“”,EstadoFinal:“ 8”}
print_r($ _ 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] =>
)
)
最佳答案
您需要获取每一行的每个元素的值。这样一来,您只会获得第一个价值。也许这会工作,未经测试:
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($_POST);
向我展示$ POST中的内容,我也可以为PHP方面提供帮助。您可以通过使用索引遍历所有数组来处理$ _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();
}
}
我添加了一个检查estadoFinal是否为空。在PHP中添加比在javascript代码中更容易。