weixin_33728708 2019-08-08 07:19 采纳率: 0%
浏览 13

PHP错误的数据库编辑

I'm setting up a dynamic webpage that will be used as a time stamp for employes. After you log in, you will write down when you started work, how long the break was, when you finished and so on.. Problem is that every time I wanna change something on the webpage, for example I want to change my break time, it won't change and shows me the user_id instead. I'm still a beginner when it comes to PHP and don't know where the problem is..here are some code snippets

<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>

  • 写回答

1条回答 默认 最新

  • Didn"t forge 2019-08-08 07:38
    关注

    In your AJAX call you have : var newValue = document.getElementById('editor' + params.currentid).value;

    In your table, for each row you have : id='editor" . $id . "'. This is a duplicate of ids.

    When doing var newValue = document.getElementById('editor' + params.currentid).value; You are getting the first element that has this id, that's why newValue is always the id (the first row holding the id value)

    I suggest you to remove that var newValue = ... but instead insert the value in the object you pass as parameter of pruefung(), such as onchange='pruefung({currentid:" . $id . ",currentfieldname:\"user_id\",value:\"" . $row['user_id'] . "\"}) for the id row, onchange='pruefung({currentid:" . $id . ",currentfieldname:\"user_id\",value:\"" . $row['start_time'] . "\"}) for the start time row, and so on...

    And then, in your AJAX call, replace var newValue = document.getElementById(...) by var newValue = params.value

    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决