I have created this simple function to be able to delete a row from a database based on the username thats inputted. However i cannot get the row to actually delete, even if the username entered is correct. Does anyone have any suggestions as to why this isnt working? (I am new to php, so sorry if there is issues with injections and/or layout / coding)
<?php
$uname = "xxxx";
$password = "xxxx";
$host = "xxxx";
$db = $uname;
$connection = mysqli_connect($host, $uname, $password, $db);
if (mysqli_connect_error()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
echo "<h1> Delete User </h1>";
if (isset($_POST["delSubmit"])) {
if ((!empty($_POST["username"]))) {
$query = "DELETE FROM login WHERE username = " . $_POST['username'] . ";";
$result = mysqli_query($connection, $query);
if ($result == false) {
echo "<p> Deleting user " . $_POST["username"] . " failed </p>";
} else {
echo "<p> The user \"" . $_POST["username"] . "\" has been deleted";
}
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form id="delForm" name="delForm" action="?" method="post">
<div>
<label for="delFormID">Please insert username to delete</label>
<input id="delFormID" name="username">
</div>
<div>
<input id="delSubmit" name="delSubmit" value="Delete User" type="submit">
</div>
</form>
</body>
</html>
<?php
mysqli_close($connection);
?>