首页 > 解决方案 > 读出数据并编辑

问题描述

我想从表中读出所有数据记录,并根据它们的“id”在“状态”中为各个数据记录赋予一个新值。

<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <title>Table</title>
    <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
    <script src="assets/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>  
    <?php
        $conn = new mysqli ("localhost", "root", "Z&r*%/Nm@X4x", "cms");
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        $sql = "SELECT id, position, status FROM data";
        $stmt = $conn->prepare($sql); 
        $stmt->execute();
        $result = $stmt->get_result();
        $data = $result->fetch_all(MYSQLI_ASSOC);

        if ($data) {
            foreach($data as $row) {
    ?>
    <form method="post">
        <div class="table-responsive d-table">
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>POSITION</th>
                        <th>STATUS</th>
                        <th><button type="submit" name="change">CHANGE STATUS</button></th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><?php echo $row['id']; ?></td>
                        <td><?php echo $row["position"]; ?></td>
                        <td><?php echo $row["status"]; ?></td>
                        <td>
                            <?php
                                if (isset($_POST['change'])) {
                                    $update = $update = "UPDATE data Set status = '2' WHERE id = {$row['id']}";
                                    mysqli_query($conn, $update);
                                    echo "Successful";
                                } elseif (!isset($_POST['change'])) {
                                    echo "Not clicked";
                                } else {
                                    echo "Failed";
                                }
                            ?>                      
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </form>
</body>
<?php
        }
    } else {
        echo "No data found!";
        $conn->close();
    }
    ?>
</html>

我的问题是,当我单击按钮时,所有记录都被更改,而不仅仅是一个。我希望有人可以帮助解决问题。

..................................................... ..................................................... ..................................................... ..................................................... .

标签: phpmysqlmysqli

解决方案


尝试使用 {} 将数组元素嵌入到字符串中并包含“id”值

$update = "UPDATE data Set status = '0' WHERE id = {$row['id']}"

否则,您的查询将解释$row[id]作为单独的项目:

UPDATE data Set status = '0' WHERE id = [id]

结果,您的所有行都更新了

还要考虑将状态更新按钮应用于每一行,如果不发送将更新哪个行 id 的参数,则无法使用一般的“更新状态”按钮仅更新一行。例如:

<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <title>Table</title>
    <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
    <script src="assets/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>  
    <?php
        $conn = new mysqli ("localhost", "root", "Z&r*%/Nm@X4x", "cms");
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        $sql = "SELECT id, position, status FROM data";
        $stmt = $conn->prepare($sql); 
        $stmt->execute();
        $result = $stmt->get_result();
        $data = $result->fetch_all(MYSQLI_ASSOC);

        if ($data) {
            foreach($data as $row) {
    ?>
    <form method="post">
        <div class="table-responsive d-table">
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>POSITION</th>
                        <th>STATUS</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><?php echo $row['id']; ?></td>
                        <td><?php echo $row["position"]; ?></td>
                        <td><?php echo $row["status"]; ?></td>
                        <td>
                            <button type="submit" name="change" value="<?php echo $row['id']; ?>">CHANGE STATUS</button>
                            <?php
                                if (isset($_POST['change']) && $_POST["change"]==$row['id']) {
                                    $update = $update = "UPDATE data Set status = '2' WHERE id = {$row['id']}";
                                    mysqli_query($conn, $update);
                                    echo "Successful";
                                } elseif (!isset($_POST['change'])) {
                                    echo "Not clicked";
                                } else {
                                    echo "Failed";
                                }
                            ?>                      
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </form>
</body>
<?php
        }
    } else {
        echo "No data found!";
        $conn->close();
    }
    ?>
</html>

推荐阅读