感谢下面的帮助,我将我的代码切换为使用 serlize(),但我仍然遇到类似的问题,仅发送最新的行,这就是我现在正在做的事情
<form id="test_table">
<table>
<thead>
<tr>
<th> ID </th>
<th> col2 </th>
<th> col3 </th>
</tr>
</thead>
<tbody><!--table body-->
<form id="form_x">
<tr>
<td> <input type="text" name="col1" default value="1"> </td>
<td> <input type="text" name="col2" default value="cat"> </td>
<td> <input type="text" name="col3" default value="dog"> </td>
</tr>
<tr>
<td> <input type="text" name="col1" default value="2"> </td>
<td> <input type="text" name="col2" default value="fish"> </td>
<td> <input type="text" name="col3" default value="rabbit"> </td>
</tr>
</tbody>
</table>
</form>
<button class="btn btn-primary" value="test_table" onclick="update(this.value)">Update</button>
<p><tt id="results"></tt></p>
<script>
function update(form_name){
var str = $( test_table ).serialize();
$( "#results" ).text( str );
$.ajax({
type: "POST",
url: "/site/pages/update.php",
data: $( "#"+form_name ).serialize(),
success : function(res){ console.log(res); }
});
}
</script>
我可以看到正在捕获的序列化数据,即
col1=1&col2=cat&col3=dog&col1=2&col2=fish&col3=rabbit
但是当我转储传递的内容时,它只看到最后一行的值
update.php
<?php var_dump($_POST); ?>
结果:
array(3) {
["col1"]=>
string(1) "2"
["col2"]=>
string(4) "fish"
["col3"]=>
string(6) "rabbit"
}
任何人都可以指导我一些建议来修复我的 update.php
页面以循环遍历 POST 数据吗?我想从该行的每个 id 更新数据库行,例如
//setup loop
$sql = "UPDATE Table SET
col2 ='".$_POST["col2"]."',
col3 ='".$_POST["col2"]."',
WHERE id='".$_POST["col1"]."' ";
即,
$sql = "UPDATE table SET
col2='cat' , col3='dog'
WHERE id=1";
$sql = "UPDATE table SET
col2='fish' , col3='rabbit'
WHERE id=2";
============================================= ============================
老问题:
我正在尝试让用户能够编辑动态创建的行(在 TD 中使用基于用户的输入),该行在单击更新按钮时发送和更新,我可以看到输入被正确捕获,但无法弄清楚如何要从 ajax 调用更新数据库,这里是(删节的)代码:
form_page.php
<table id="table_name">
<thead>
<tr>
<th>ID</th>
<th>Col 2</th>
<th>Col 3</th>
</tr>
与生成表格行相关的 phpcode:
<?php
echo '<form id="" method="POST" action="" >';
$sql= "SELECT * FROM tablename";
$stmt= $conn->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll();
foreach ($results as $row) {
echo '<td>';
echo 'ItemID_'.$row['ID'].'';
echo '</td>';
echo '<td>';
echo ' <input class="update_value" type="text" id="ItemID_'.$row['ID'].'" default value="'.$row['col2'].'">';
echo '</td>';
echo ' <input class="update_value" type="text" id="ItemID_'.$row['ID'].'" default value="'.$row['col3'].'">';
echo '</td>';
echo'</tr'>;
}
?>
<button type="button" id="update_button">Update</button>
</form>
</table>
可编辑 col 2/3 的结果:
ID | col2 | col3
1 | lorem (editable) | ipsum (editable)
2 | dolor (editable) | sit amet (editable)
Update (Button)
代码看起来像这样:
<table id="table_name">
<thead>
<tr>
<th>ID</th>
<th>Col 2</th>
<th>Col 3</th>
</tr>
</thead>
<tbody>
<form id="" method="POST" action="">
<tr>
<td>
ItemID_1
</td>
<td> <input class="update_value" type="text" id="ItemID_1" default value="lorem">
</td>
<td> <input class="update_value" type="text" id="ItemID_2" default value="ipsum">
</td>
</tr>
</tbody>
<button type="button" id="update_button">Update</button>
这是我用来将其发送到更新页面的 ajax
<script>
$(document).ready(function(){
$("#update_button").click(function(e) {
e.preventDefault();
var values = {};
$('input.update_value').each(function(n, user_input){
values[ $(user_input).attr('id') ] = $(user_input).val();
//used to test and make sure its passing id vals to values
//alert(JSON.stringify(values, null, 4));
});
$.ajax({
type: "POST",
url: "/site/pages/update.php",
data: { updatefrominputs: values }
});
});
});
</script>
上面的警报(用于测试)显示:
localhost says
{
"ItemID_1": "lorem"
"ItemID_1": "ipsum"
}
到目前为止还不错,但是再点击一次就会写“lorem”
localhost says
{
"ItemID_1": "ipsum"
"ItemID_2": "dolor"
}
这是第一个问题,它重写了要发送的“lorem”值
这是第二个问题,也是我目前卡住的地方,发送和更新数据库
通过将此添加到 ajax,我可以看到正在发布 ajax:
success: function (data) {
alert("sent");
}
update.php
文件代码:
<? php
include connect.php //database connection script (don't think I need to call it)
foreach($_POST['updatefrominputs'] as $id=>$value)
$sqlQuery = "UPDATE tablename SET
//col2=col2inputvalue
//col3=col3inputvalue
WHERE
//col1id=the NUMBER without the ID_part?
";
$sqlQueryStmt = $conn->prepare($sqlQuery);
$sqlQueryStmt->execute();
?>
我遇到的问题是我似乎无法让它更新数据库,我不确定如何填写下面的 UPDATE 子句。此外,它甚至感觉不到它发布到此页面,但成功功能说它发布了??
此外,我如何才能获得要在 WHERE
子句中使用的数字?我可以编辑上面的代码,只给我 ID 号,然后使用 col1ID=col1passedID
但如果我在多个表单(在同一页面上)使用此过程,我最终会得到重复的 ID .例如
<form 1>
foreach ($results1 as $row1) {
echo ' <input class="update_value" type="text" id="'.$row1['ID'].'" default value="'.$row['col2'].'">';
}
</form 1>
<form 2>
foreach ($results2 as $row2) {
echo ' <input class="update_value" type="text" id="'.$row2['ID'].'" default value="'.$row2['col2'].'">';
}
</form 2>
会输出:
<form 1>
<input class="update_value" type="text" id="1" default value="lorem">
</form 1>
<form 2>
<input class="update_value" type="text" id="1" default value="ipsum">
</form 2>
我能想到的一个解决方案是像这样发送 ajax:
var updatedetals= {
id: $("#ItemID_1").val(),
col1: $("#col2").val(),
col2: $("#col2").val()
}
$.ajax({
type: "POST",
url: "/site/pages/update.php",
data:updatedetals,
dataType: 'JSON',
success: function(JSON){
refreshResults();
}
});
但这也不会给我唯一的 ID,我必须对每一行进行 ajax 调用吗?
有没有更好的方法来迭代每个唯一生成的行,获取 TD 输入值并将这些更新的值发送到数据库?
最佳答案
您需要在您的 html 中使用 name="col1[]"
这会将所有值发送到一个数组中。因为您有多个具有相同名称的输入,所以默认情况下 php 获取最后一个。如果你添加 []
你可以获取像 $_POST["col1"][0]
这样的值。
然后你也可以遍历像
for($i=0;$i<count($_POST["col1"]);$i++){
$col1 = $_POST["col1"][$i];
$col2 = $_POST["col2"][$i];
$col3 = $_POST["col3"][$i];
//use the variables in update query
}
关于php - 循环遍历序列化数据以将更新插入 MySQL DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52885956/