php集成修改数据库的字段

1.界面效果

在这里插入图片描述

2.代码

<?php

echo '<form action="" method="post">
  

    <label for="table">表名:</label>
    <input type="text" id="table" name="table"><br>

    <div id="fieldsContainer">
        <div class="fieldGroup">
            <label for="field">字段:</label>
            <input type="text" class="field" name="field[]">
            <label for="value">值:</label>
            <input type="text" class="value" name="value[]"><br>
        </div>
    </div>
    <button type="button" id="addField">+</button><br>

    <label for="conditionField">条件字段 (where):</label>
    <input type="text" id="conditionField" name="conditionField">
    <label for="conditionValue">条件值:</label>
    <input type="text" id="conditionValue" name="conditionValue"><br>

    <input type="submit" value="提交">
</form>';

echo '<script>
document.addEventListener("DOMContentLoaded", function(){
    document.getElementById("addField").addEventListener("click", function(){
        var container = document.getElementById("fieldsContainer");
        var fieldGroup = document.createElement("div");
        fieldGroup.classList.add("fieldGroup");
        fieldGroup.innerHTML = \'<label for="field">字段:</label><input type="text" class="field" name="field[]"><label for="value">值:</label><input type="text" class="value" name="value[]"><br>\';
        container.appendChild(fieldGroup);
    });

    var populateFields = function() {
        if (localStorage.getItem("table")) {
            document.getElementById("table").value = localStorage.getItem("table");
        }
        if (localStorage.getItem("conditionField")) {
            document.getElementById("conditionField").value = localStorage.getItem("conditionField");
        }
        if (localStorage.getItem("conditionValue")) {
            document.getElementById("conditionValue").value = localStorage.getItem("conditionValue");
        }
        var fields = JSON.parse(localStorage.getItem("fields") || "[]");
        var values = JSON.parse(localStorage.getItem("values") || "[]");
        for (var i = 0; i < fields.length; i++) {
            if (i > 0) {
                document.getElementById("addField").click();
            }
            document.getElementsByClassName("field")[i].value = fields[i];
            document.getElementsByClassName("value")[i].value = values[i];
        }
    };

    populateFields();

    document.querySelector("form").addEventListener("submit", function() {
        localStorage.setItem("table", document.getElementById("table").value);
        localStorage.setItem("conditionField", document.getElementById("conditionField").value);
        localStorage.setItem("conditionValue", document.getElementById("conditionValue").value);
        var fields = Array.from(document.getElementsByClassName("field")).map(function(input) { return input.value; });
        var values = Array.from(document.getElementsByClassName("value")).map(function(input) { return input.value; });
        localStorage.setItem("fields", JSON.stringify(fields));
        localStorage.setItem("values", JSON.stringify(values));
    });
});

</script>';

echo '<script>
    if(window.history.replaceState) {
        window.history.replaceState(null, null, window.location.href);
    }
</script>';



if ($_SERVER["REQUEST_METHOD"] == "POST") {
   
    $table = $_POST['table'];
    $fields = $_POST['field']; // Assuming 'field' is an array of field names
    $values = $_POST['value']; // Assuming 'value' is an array of corresponding values

    $conditionField = $_POST['conditionField'];
    $conditionValue = $_POST['conditionValue'];

    $updateFields = "";
    for ($i = 0; $i < count($fields); $i++) {
        $field = $fields[$i];
        $value = $values[$i];
        if (!empty($field) && !empty($value)) {
            if ($updateFields !== "") {
                $updateFields .= ", ";
            }
            $updateFields .= "$field = '$value'";
        }
    }

    // Database connection,这个要修改成你的数据库的密码和账号和表名
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "zbsv20";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql = "UPDATE $table SET $updateFields WHERE $conditionField='$conditionValue'";

    // echo '<pre>';
// var_dump($sql);
// die('end');

    if ($conn->query($sql) === TRUE) {
        echo "Record updated successfully";
    } else {
        echo "Error updating record: " . $conn->error;
    }

    $conn->close();
}

3.功能

1.提交的字段不会刷新消失
2.可复制字段,一次改多个
3.方便操作,不需要在数据库软件操作

相关推荐

  1. 数据库中如何修改和删除字段

    2024-03-10 14:58:07       43 阅读
  2. SQL Server修改数据字段名方法

    2024-03-10 14:58:07       31 阅读
  3. mybatis查询修改mysqljson字段

    2024-03-10 14:58:07       32 阅读
  4. PHP 与 MySQL 数据库集成教程

    2024-03-10 14:58:07       58 阅读
  5. SQLAlchemy修改postgres表jsonb字段失效

    2024-03-10 14:58:07       15 阅读
  6. 前端请求patch接口,只传入已修改字段字段

    2024-03-10 14:58:07       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-10 14:58:07       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-10 14:58:07       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-10 14:58:07       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-10 14:58:07       20 阅读

热门阅读

  1. RabbitMQ如何实现消费端限流

    2024-03-10 14:58:07       23 阅读
  2. 小米汽车上市进入倒计时,已开启内部试驾

    2024-03-10 14:58:07       24 阅读
  3. ImportError: cannot import name ‘InterpolationMode‘

    2024-03-10 14:58:07       24 阅读
  4. Anaconda和PyCharm比较

    2024-03-10 14:58:07       20 阅读
  5. Linux/Ubuntu/Debian基本命令:光标移动命令

    2024-03-10 14:58:07       19 阅读