Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript PHP错误的数据库编辑_Javascript_Php_Ajax - Fatal编程技术网

Javascript PHP错误的数据库编辑

Javascript PHP错误的数据库编辑,javascript,php,ajax,Javascript,Php,Ajax,我正在建立一个动态网页,作为员工的时间戳。登录后,你会写下你什么时候开始工作,休息多长时间,什么时候结束等等。。 问题是,每当我想更改网页上的某些内容时,例如我想更改休息时间,它不会更改,而是显示用户id。 说到PHP,我还是个初学者,不知道问题出在哪里。下面是一些代码片段 <div class="header"> <ul> <li> <p class="text">HourBook</p>

我正在建立一个动态网页,作为员工的时间戳。登录后,你会写下你什么时候开始工作,休息多长时间,什么时候结束等等。。 问题是,每当我想更改网页上的某些内容时,例如我想更改休息时间,它不会更改,而是显示用户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>


  • HourBook

名称 开始时间 暂停 结束时间 评论 期间
在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()
的参数插入您传递的对象中,例如
onchange='pruefung({currentid:'.$id.”,currentfieldname:\'user\u id\',value:\'.$row['user\u id'.\'})
对于id行,
onchange='pruefung({currentid:'.$id:'.$id.”,currentfieldname:\“user\u id\”,值:\“”.$row['start\u time'].“\”})
用于开始时间行,依此类推


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

您遇到了什么错误?@Jaymin没有错误消息,它会在每次安装其他内容时上载用户id。因此,每次键入内容时,页面被刷新,只显示用户id是这样吗?@Jaymin我有我的专栏,每次我在网页上的这些互动专栏中写一些东西并刷新它,专栏中的文本将与用户id相同。你是用ajax传递数据还是像普通网页一样发布数据?不客气,请记住,ID必须是唯一的:)