简体   繁体   中英

PHP deleting a row with a string

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

// 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;
}

Spot the SQL error:

username = username=?

Should be

username = ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM