-4

在选择它动态显示从数据库中获取的值,我想要做的是用户从下拉列表中选择值,即实际在数据库中的值,当用户提交删除时,mysql删除该选定的用户。为什么它不管用 ?

<?php

    // Database Constants
    define("DB_SERVER", "localhost");
    define("DB_NAME", "audit");
    define("DB_USER", "root");
    define("DB_PASS", "123456");

    // Create a database connection
    $connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
    if (!$connection) {
    die("Database connection failed: " . mysql_error());
    }

    // Select a database to use 
    mysql_select_db(DB_NAME,$connection);
?>

<html>
<head>
    <title>Delete Users</title>
</head>
<body>
<?php 
    $username = $_POST['react'];

    if(!empty($username])) {
        $query= "DELETE FROM users WHERE username='$username'";
        $result = mysql_query($query,$connection);
        if(mysql_num_rows($result)) {
            print("<strong>$user</strong>Successfully Deleted<p>");
        }
        else {
            print("<strong>no users are available to delete yet, sorry. </strong><p>");
        }
    }
?>

<form method="post" action="Delete_user.php"><div align="center"><center>                                              <p>Delete users
    <input type="hidden" name="react" value="delete_user
           <select name="user" size="1">
    <?php 
        $query = "SELECT username FROM users ORDER BY username";
        $result = mysql_query($query,$connection);
        if(mysql_num_rows($result)){
            //we have atleast one user,so show all users as options in select
            while ($rows = mysq_fetch_row($result))
            {
                print("<option value=\"$rows[0]\">$rows[0]</option>");
            }
        }
        else {
            print("<option value=\"\">Please Select User</option>");
        }
    ?>
    </select><input type="submit" value="submit"></center></p></div>
</body>
</html> 
4

2 回答 2

1

检查你的第二个if()陈述。

if(!empty($username])) { ...

那里有一个方括号。

接下来,在靠近底部的表单中,您有:

<input type="hidden" name="react" value="delete_user

这应该是:

<input type="hidden" name="react" value="delete_user" />

其他一切看起来都很好。尝试注意您的错误消息。

于 2013-08-02T16:35:27.000 回答
0

此行有错误

while ($rows = mysq_fetch_row($result))

改成

while ($rows = mysql_fetch_row($result))
于 2013-08-02T18:35:00.360 回答