简体   繁体   English

如何在我的HTML表中添加一个删除和编辑功能,该表填充了我的MySQL数据库中的信息?

[英]How to add a delete and edit function into my HTML table which is filled with info from my MySQL database?

I've made this simple PHP/HTML table which shows info in my database, now i want to add a delete and edit function using the SQL DELETE and EDIT functions. 我已经制作了这个简单的PHP / HTML表格,在我的数据库中显示信息,现在我想使用SQL DELETE和EDIT函数添加删除和编辑功能。

How can i implent this so that every row has a delete button and edit button automaticly when its added to the DB? 我如何实现这一点,以便每行添加到数据库时自动有一个删除按钮和编辑按钮?

Table

<table style="border: 1px solid black;">
    <tr>
        <th style="border-bottom: 1px solid black;">CI nummer</th>
        <th style="border-bottom: 1px solid black;">Aankoopdatum</th>
        <th style="border-bottom: 1px solid black;">Serienummer</th>
        <th style="border-bottom: 1px solid black;">Merk</th>
        <th style="border-bottom: 1px solid black;">Model</th>
        <th style="border-bottom: 1px solid black;">OS</th>
        <th style="border-bottom: 1px solid black;">Eigenaar</th>
        <th style="border-bottom: 1px solid black;">Locatie</th>
        <th style="border-bottom: 1px solid black;">Bewerken</th>
        <th style="border-bottom: 1px solid black;">Verwijderen</th>
    </tr>
        <?php
        $conn = mysqli_connect("localhost", "root", "", "teradruk");

        if (mysqli_connect_errno()) {
            echo "Kan geen verbinding maken met de database!";
            exit();
        }

        $sqli = "SELECT * FROM `hardware`";

        if ($stmti = $conn->prepare($sqli)) {
            if (!$stmti->execute()) {
                echo "cant exec: <br> " . $stmti->error;
                exit();
            } else {
                $stmti->bind_result($cinummer, $aankoopdatum, $serienummer, $merk, $model, $os, $eigenaar, $locatie, $applicatie);
                $stmti->store_result();

                while($stmti->fetch()) {
                    echo "<tr>
                        <td>$cinummer</td>
                        <td>$aankoopdatum</td>
                        <td>$serienummer</td>
                        <td>$merk</td>
                        <td>$model</td>
                        <td>$os</td>
                        <td>$eigenaar</td>
                        <td>$locatie</td>
                        </tr>";
                }

            }
        } else {
            echo "can't prepare: <br> " . $stmti->error;
            exit();
        }
        ?>
</table>

I just update the code. 我只是更新代码。 but I did not create a EDIT form in HTML and send values, I have write PHP codes 但我没有在HTML中创建EDIT表单并发送值,我已经编写了PHP代码

    <table style="border: 1px solid black;">
    <tr>
        <th style="border-bottom: 1px solid black;">CI nummer</th>
        <th style="border-bottom: 1px solid black;">Aankoopdatum</th>
        <th style="border-bottom: 1px solid black;">Serienummer</th>
        <th style="border-bottom: 1px solid black;">Merk</th>
        <th style="border-bottom: 1px solid black;">Model</th>
        <th style="border-bottom: 1px solid black;">OS</th>
        <th style="border-bottom: 1px solid black;">Eigenaar</th>
        <th style="border-bottom: 1px solid black;">Locatie</th>
        <th style="border-bottom: 1px solid black;">Bewerken</th>
        <th style="border-bottom: 1px solid black;">Verwijderen</th>
        <th style="border-bottom: 1px solid black;">Options</th>
    </tr>
        <?php
        $conn = mysqli_connect("localhost", "root", "", "teradruk");

        if (mysqli_connect_errno()) {
            echo "Kan geen verbinding maken met de database!";
            exit();
        }

        if($_GET['action']=="delete")
        {
            $sql_delete = mysql_query("DELETE FROM hardware WHERE PRIMARY_KEY_OF_hardware_TABLE = ".$_GET['id']." ");
        }
        elseif($_GET['action']=="edit")
        {


            $sql_update = "UPDATE hardware SET cinummer = 'New Number' WHERE PRIMARY_KEY_OF_hardware_TABLE = ".$_GET['id']." ";
        }


        $sqli = "SELECT * FROM `hardware`";

        if ($stmti = $conn->prepare($sqli)) {
            if (!$stmti->execute()) {
                echo "cant exec: <br> " . $stmti->error;
                exit();
            } else {
                $stmti->bind_result($cinummer, $aankoopdatum, $serienummer, $merk, $model, $os, $eigenaar, $locatie, $applicatie);
                $stmti->store_result();

                while($stmti->fetch()) {
                    echo "<tr>
                        <td>$cinummer</td>
                        <td>$aankoopdatum</td>
                        <td>$serienummer</td>
                        <td>$merk</td>
                        <td>$model</td>
                        <td>$os</td>
                        <td>$eigenaar</td>
                        <td>$locatie</td>
                        <td><a href='filename.php?id=".$PRIMARY_KEY_OF_hardware_TABLE."&action=delete'>Delete</a> | <a href='filename.php?id=".$PRIMARY_KEY_OF_hardware_TABLE."&action=edit'>Edit</a></td>
                        </tr>";
                }

            }
        } else {
            echo "can't prepare: <br> " . $stmti->error;
            exit();
        }
        ?>
</table>

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM