首页 > 解决方案 > 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>

标签: javascriptphpajax

解决方案


在您的 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


推荐阅读