简体   繁体   English

使用mysql和php从HTML表中删除记录

[英]Delete a record from HTML table using mysql and php

Hey there i am new to PHP and need to delete a record from MYSQL TABLE where my username resides in html table! 嘿,我是PHP新手,需要从MYSQL TABLE删除一条记录,其中我的用户名位于html表中! my code for fetching mysql table data into html table is: 我将mysql表数据提取到html表中的代码是:

   while($data=mysqli_fetch_array($result)){
                                    $count+=1;
                                    echo "<tr>";

                                    echo "<td>";
                                    echo "<p>";
                                        echo $count;
                                        echo "</p>";
                                    echo "</td>";

                                        echo "<td>";
                                        echo "<p>";
                                            echo $data['myusername'];
                                        echo "</p>";
                                        echo "</td>";

                                        echo "<td>";
                                        echo "<p>";
                                            echo $data['logincount'];
                                        echo "</p>";
                                        echo "</td>";

                                        echo "<td>";
                                        echo "<p>";
                                            echo $data['signindate'];
                                        echo "</p>";
                                        echo "</td>";

                                        echo "<td>";
                                        echo "<p>";
                                            echo $data['signupdate'];
                                        echo "</p>";
                                        echo "</td>";

                                        echo "<td>";
                                        echo "<p>";
                                            echo "<a href='deluser.php?id=" . $data['myusername'] . "'>Del</a>"; //here i want to use this link to delete a user

                                        echo "</p>";
                                        echo "</td>";

                                    echo "</tr>";
                                }

my deluser.php is: 我的deluser.php是:

<?php
        //$user=$_GET['myusername'];
        $isConnected=mysqli_connect('localhost','root','','mydb');
        if($isConnected){
            if (isset($_GET["myusername"])) {
            $query = "DELETE FROM users WHERE myusername = " . $_GET["myusername"];
            $result = mysqli_query($con, $query);
            // Check the result and post confirm message
            if(!$result){
                echo 'error'.mysqli_error($isConnected);
            }
            else{
                echo 'success';
            }

            }
        }

    ?>

suppose that the connection have been set and then i want to delete a record using a wildcard ie myusername='value for html table'! 假设已经设置了连接,然后我想使用通配符删除记录,即myusername ='html表的值'!

The problem is that nothing is showing to me nor an error neither success so is it me doing something wrong can somebody please help me! 问题是什么都没有显示给我,也没有错误,也没有成功,所以我做错了吗?有人可以帮助我!

$result = mysqli_query($con, $query);

mysqli_query() function first argument must be connection link, why you use $con variable? mysqli_query()函数的第一个参数必须是连接链接,为什么要使用$con变量? Your connection link to database in $isConnected variable. 您的连接链接到$isConnected变量中的数据库。 Try use this: 试试这个:

$result = mysqli_query($isConnected, $query);

And if your myusername value is string, you need take this value in single quotes: 而且,如果您的myusername值为字符串,则需要将此值用单引号引起来:

$query = "DELETE FROM users WHERE myusername = '" . $_GET["myusername"] . "'";

For update information in database, you need use POST method, GET method is good for information required but not for update, it is not safe. 对于数据库中的更新信息,您需要使用POST方法, GET方法适合于所需的信息,但不能用于更新,因此不安全。

It looks like you set up your query incorrect. 看起来您设置的查询不正确。 Should be: 应该:

$query = "DELETE FROM users WHERE myusername = '" . $_GET["myusername"] . "'";

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

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