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 . "'。这是id 的副本

执行var newValue = document.getElementById('editor' + params.currentid).value;时,您将获得具有此id的第一个元素,这就是为什么newValue始终是id(第一行保存id值(

我建议您删除该var newValue = ...而是将值插入您作为pruefung()参数传递的对象中,例如 id 行的onchange='pruefung({currentid:" . $id . ",currentfieldname:"user_id",value:"" . $row['user_id'] . ""}),开始时间行的onchange='pruefung({currentid:" . $id . ",currentfieldname:"user_id",value:"" . $row['start_time'] . ""}),等等......

然后,在 AJAX 调用中,将var newValue = document.getElementById(...)替换为var newValue = params.value

最新更新