JustMatthew JustMatthew - 1 month ago 8
SQL Question

Appropriate sql command to delete a row from table using form?

I want to delete one row from one of my tables, based on a form -> "username".Let's assume, i have a log in page, where i write into the fields the apropriate username, and password based on a database which contains these values. After log in, i want to log out. And then I want to delete from the table the username and passwrod i used to sign in. How can i do it? Here's my code, it's unfortunately delete all the rows from the table. I have tried many ways... please help.

$sql="DELETE FROM login WHERE username='username'" ;
$result = mysqli_query($sqlconnection,$sql) or die...

Answer

assume that you receive the username value by a form by post and assign the value to a vars

$username = $_SESSION['login_user'];

then you can pass the value to you query this way

$sql="DELETE FROM login WHERE username='$username';" ;

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

// sql to delete a record
$sql="DELETE FROM login WHERE username='$username';" ;

if ($conn->query($sql) === TRUE) {
    echo "Record deleted successfully";
} else {
    echo "Error deleting record: " . $conn->error;
}