javascript - PHP错误的数据库编辑
问题描述
我正在设置一个动态网页,该网页将用作员工的时间戳。登录后,你会写下你什么时候开始工作,休息多长时间,什么时候完成等等。问题是每次我想在网页上改变一些东西,例如我想改变我的休息时间,它不会改变,而是向我显示 user_id。当谈到 PHP 时,我仍然是初学者,不知道问题出在哪里..这里有一些代码片段
<div class="header">
<ul>
<li>
<p class="text">HourBook</p>
</li>
</ul>
</div>
<table class="table table-striped table-bordered">
<tr>
<th class="luecke1">Name</th>
<th class="luecke2">Start Time</th>
<th class="luecke3">Pause</th>
<th class="luecke4">End Time</th>
<th class="luecke5">Comments</th>
<th class="luecke6">Duration</th>
</tr>
<?php
$connect = new mysqli('localhost', 'root', '', 'hourbook');
$result = $connect->query("SELECT * FROM data");
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
echo "<tr>";
echo "<td class='luecke1'><input style='border:none; width:100%; background:rgba(0,0,0,0)' contenteditable='true' id='editor" . $id . "' value='" . $row['user_id'] . " ' onchange='pruefung({currentid:" . $id . ",currentfieldname:\"user_id\"})'></input></td>";
echo "<td class='luecke2'><input style='border:none; width:100%; background:rgba(0,0,0,0)' contenteditable='true' id='editor" . $id . "' value='" . $row['start_time'] . " ' onchange='pruefung({currentid:" . $id . ",currentfieldname:\"start_time\"})'></input></td>";
echo "<td class='luecke3'><input style='border:none; width:100%; background:rgba(0,0,0,0)' contenteditable='true' id='editor" . $id . "' value='" . $row['pause_time'] . " ' onchange='pruefung({currentid:" . $id . ",currentfieldname:\"pause_time\"})'></input></td>";
echo "<td class='luecke4'><input style='border:none; width:100%; background:rgba(0,0,0,0)' contenteditable='true' id='editor" . $id . "' value='" . $row['end_time'] . " ' onchange='pruefung({currentid:" . $id . ",currentfieldname:\"end_time\"})'></input></td>";
echo "<td class='luecke5'><input style='border:none; width:100%; background:rgba(0,0,0,0)' contenteditable='true' id='editor" . $id . "' value='" . $row['comments'] . " ' onchange='pruefung({currentid:" . $id . ",currentfieldname:\"comments\"})'></input></td>";
echo "<td class='luecke6'><input style='border:none; width:100%; background:rgba(0,0,0,0)' contenteditable='true' id='editor" . $id . "' value='" . $row['total_time'] . " ' onchange='pruefung({currentid:" . $id . ",currentfieldname:\"total_time\"})'></input></td>";
}
} else {
echo "No results!";
}
?>
<script>
var oldValue;
function pruefung(params) {
var newValue = document.getElementById('editor' + params.currentid).value; //editor id verknüpfung falsch?
if (oldValue == newValue) {
console.log("no changes");
} else {
console.log("changes");
$.ajax({
url: "update.php",
type: "POST",
data: {
value: newValue,
id: params.currentid,
fieldname: params.currentfieldname
},
success: function(data) {
alert("Saved successfully!");
document.location.reload(true);
},
error: function(data) {
alert("error: " + data);
}
})
}
}
</script>
</table>
解决方案
在您的 AJAX 调用中,您有:var newValue = document.getElementById('editor' + params.currentid).value;
在您的表格中,对于您拥有的每一行 : id='editor" . $id . "'
。这是ids 的副本。
当var newValue = document.getElementById('editor' + params.currentid).value;
你得到第一个具有这个 id 的元素时,这就是为什么newValue
总是 id (第一行保存 id 值)
我建议您删除它,var newValue = ...
而是将值插入您作为参数传递的对象中pruefung()
,例如onchange='pruefung({currentid:" . $id . ",currentfieldname:\"user_id\",value:\"" . $row['user_id'] . "\"})
id 行、onchange='pruefung({currentid:" . $id . ",currentfieldname:\"user_id\",value:\"" . $row['start_time'] . "\"})
开始时间行等...
然后,在您的 AJAX 调用中,替换var newValue = document.getElementById(...)
为var newValue = params.value
推荐阅读
- python - Python:DF 中特定部分的从行到列
- html - CSS Grid - 在某些 div 上设计不相等的高度
- python - PyCharm 没有在我的 Mint 19 上检测到 Python 2.7
- python - 从顶部打印表格(列名等)然后向下滚动(Python)?
- php - 如何在双引号之间打印函数值?
- html - 如何添加渐变动画?
- javascript - 我想为 C# MVC(客户端技术 javascript、jquery)实现 chrome 管理密码之类的功能
- android - 如何从 Android Q 上的 MediaStore 获取文件的时间戳
- html - Chrome 显示混合内容,但协议实际上匹配
- haskell - 如何在 Haskell 中“打破”列表理解?