1

I am trying to delete a row that matches a string that is passed in to the method.

$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$data = array($_POST["username"]);


$stmt = $conn->prepare("DELETE FROM Table WHERE username = username=? ");
$stmt->execute($data);

I tried a few combinations of the SQL statement but cannot get one to work

0

2 Answers 2

1
// Store user input in a variable
$data =  $_POST["username"];

// Prepare the query
$stmt = $conn->prepare("DELETE FROM Table WHERE username=:username");

// Bind the value
$stmt->bindValue(':username', $data, PDO::PARAM_STR);

// Execute the query
$success = $stmt->execute();

// If query succeeded, display the number of affected rows
if ($success) {
    $affected_rows = $stmt->rowCount();
    echo $affected_rows;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Spot the SQL error:

username = username=?

Should be

username = ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.