当我按删除按钮时,它显示空白页

问题描述 投票:0回答:1

我有表格并且连接了MySQL,所以每当我填写时它都会显示在mySQL中。下一步是在我的index.php 中有一个目录,即我所拥有的。但现在,当我想按删除按钮时,它会显示总页。

这是我的index.php

<?php

if (isset($_POST["submit"])) {
    $ime = $_POST["ime"];
    $prezime  = $_POST["prezime"];
    $pozicija  = $_POST["pozicija"];


    if (!empty($ime) && !empty($prezime) && !empty($pozicija)) {

        /*host, username, password, dbname*/
        $link = mysqli_connect("localhost", "root", "", "forma");

        // Check connection
        if ($link === false) {
            die("ERROR: Could not connect. " . mysqli_connect_error());
        }


        // Attempt insert query execution
        $sql = "INSERT INTO forma (Ime, Prezime, Poziicija) VALUES ('$ime', '$prezime' , '$pozicija')";
        if (mysqli_query($link, $sql)) {
            echo "Records inserted successfully." . "<br>";
        } else {
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
        }

        $podaci = "SELECT * FROM forma";
        $rezultat = mysqli_query($link, $podaci);

        if (!$rezultat) {
            die(mysqli_error($link));
        }
        if (mysqli_num_rows($rezultat) > 0) {
            foreach ($rezultat as $rez) {

                foreach ($rez as $key => $value) {

                    echo ucfirst($key) . ": " . $value . "<br>";
                }
            }
        } ?>

        <!DOCTYPE html>
        <html lang="en">
        <?php session_start(); ?>

        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Document</title>
        </head>

        <body>
            <?php
            if (isset($_SESSION['status'])) {
            ?>
                <div class="alert alert-warning alert-dismissible fade show" role="alert">
                    <strong>Hey!</strong> <?php echo $_SESSION['status']; ?>
                    <button type="button" class="btn" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            <?php
                unset($_SESSION['status']);
            }
            ?>
            <form action="delete.php" method="POST">
                <table class="table">
                    <tbody>
                        <tr>
                            <th>
                                <button type="submit" name="delete" class="btn-delete">Delete</button>
                            </th>
                            <th>ID</th>
                            <th>Ime</th>
                            <th>Prezime</th>
                            <th>Pozicija</th>
                        </tr>
                    </tbody>

                    <tbody>

                        <?php
                        $con = mysqli_connect("localhost", "root", "", "forma");

                        $query = "SELECT * FROM forma";
                        $query_run  = mysqli_query($con, $query);

                        if (mysqli_num_rows($query_run) > 0) {
                            foreach ($query_run as $row) {
                        ?>
                                <tr>
                                    <td>
                                        <input type="checkbox" name="delete_id[]" value="<?= $row['ID']; ?>">
                                    </td>
                                    <td><?= $row['ID']; ?></td>
                                    <td><?= $row['Ime']; ?></td>
                                    <td><?= $row['Prezime']; ?></td>
                                    <td><?= $row['Poziicija']; ?></td>
                                </tr>
                            <?php
                            }
                        } else {
                            ?>
                            <tr>
                                <td colspan="5">No Record Found</td>
                            </tr>
                        <?php
                        }
                        ?>
                    </tbody>
                </table>
            </form>
        </body>

        </html>

<?php
        // Close connection
        mysqli_close($link);
    }
    
} 

这是我的 insert.php,其中只是表单

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Form</title>
</head>

<body>
    <form action="index.php" method="post">
        <label for="ime">Ime</label>
        <input type="text" name="ime" required><br>
        <label for="prezime">Prezime</label>
        <input type="text" name="prezime" required><br>
        <label for="pozicija">Pozicija</label>
        <input type="text" name="pozicija" required><br>
        <button type="submit" name="submit">Submit</button><br>
    </form>

    
</body>

</html>

这是我的delete.php

    <?php
    //delete

    session_start();
    $con = mysqli_connect("localhost", "root", "", "forma");
    if (isset($_POST['delete'])) {
        $all_id = $_POST['delete_id'];
        $extract_id = implode(',', $all_id);
        //echo $extract_id;

        $query = "DELETE FROM forma WHERE id IN($extract_id) ";
        $query_run = mysqli_query($con, $query);

        if ($query_run) {
            $_SESSION['status'] = "Multiple Data Deleted Successfully";
            header("Location: index.php");
        } else {
            $_SESSION['status'] = "Multiple Data Not Deleted";
            header("Location: index.php");
        }
    }

所以我只是想了解我的下一步是什么,或者我做错了什么。谢谢您的帮助。

php mysql forms sql-delete
1个回答
0
投票

在您的删除页面中,我认为您的查询中缺少逗号:

$query = "从格式中删除 WHERE id IN ($extract_id)";

© www.soinside.com 2019 - 2024. All rights reserved.