-4

In select it dynamically shows value which is fetched from database,what i wanted to do is user select the value from dropdown list i.e-the values that are actually in the database and when users submit the delete,mysql deletes that selected user.why it is not working ?

<?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> 
putvande
  • 15,068
  • 3
  • 34
  • 50

2 Answers2

1

Check your second if() statement.

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

There's a square bracket there.

Next, in your form near the bottom, you have:

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

This should be:

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

Everything else looks okay. Try to pay attention to your error messages.

theftprevention
  • 5,083
  • 3
  • 18
  • 31
  • Also he is getting a value from `$_POST` without knowing whether the key exists. `$username = isset($_POST['react']) ? $_POST['react'] : NULL;` – AmazingDreams Aug 02 '13 at 16:37
  • ok now there is no mysql error but there is no options in the dropdown,there is a dropdown option but its blank,thoughin my table users there is four user already created my users table structure is following : TABLE users- id, username, password. – user2603903 Aug 02 '13 at 17:26
  • I just looked again, and it seems you misspelled a function. `mysq_fetch_row($result)` is missing an "L" and should be `mysql_fetch_row($result)`. – theftprevention Aug 02 '13 at 17:39
  • ok now this page is ok when i select user and submit it the following error come "Warning:mysql_num_rows()expects parameter 1 to be resource,boolean given in C:\www\login\delete.php on line 15" – user2603903 Aug 02 '13 at 18:28
  • That means one of your SQL queries returned `false` (meaning it failed), and that value of `false` is being sent to one of your `mysql_num_rows` functions. Take a look at your query indicated on line 15, and check the contents of `mysql_error()`. – theftprevention Aug 02 '13 at 18:53
  • i am not understanding what to do now,what code should i change ? – user2603903 Aug 02 '13 at 22:29
0

Error in this line

while ($rows = mysq_fetch_row($result))

change to

while ($rows = mysql_fetch_row($result))
SiRiC
  • 13
  • 5
  • "Warning:mysql_num_rows()expects parameter 1 to be resource,boolean given in C:\www\login\delete.php on line 15" – user2603903 Aug 02 '13 at 21:59