clp
clp

Reputation: 53

Deleting using PHP

I have a select dropdown list that contains all existing usernames found in my table. I want to delete an entry from my table by selecting the corresponding username. I can't seem to find the error in my php file...I get the "Successfully deleted" message even if the entry is still there. Want to know what's wrong with my php. Thanks.

Here's the concerned portion of the View:

<form action="deleting.php" method="post">
<select id="username">
    <option ng-repeat="user in users">
      {{user.username}}
    </option>
 </select>
<input type="submit" value="Delete"/>
</form>

Here's my deleting.php file

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "abc";


$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$deletecontent= isset( $_POST["username"] ) ? $_POST["username"] : "null" ;


$sql = "DELETE from users where username='$deletecontent'";


if (mysqli_query($conn, $sql)) {
    echo "Successfully deleted";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}


mysqli_close($conn);
?>

Upvotes: 1

Views: 42

Answers (3)

user5570620
user5570620

Reputation:

You can get value in $_POST only when there is name attribute in your input. so try

<select id="username" name="username">

Upvotes: 4

Nana Partykar
Nana Partykar

Reputation: 10548

Set name="username" in <select>

and,

set value attribute in <option>.

<form action="deleting.php" method="post">
    <select id="username" name="username">
          <option ng-repeat="user in users" value="{{user.username}}">
            {{user.username}}
          </option>
     </select>
    <input type="submit" value="Delete"/>
</form>

Upvotes: 0

Jenz
Jenz

Reputation: 8369

You have to add name attribute to select option. Then only it can be accessed using $_POST.

<select id="username" name = "username">

Upvotes: 2

Related Questions