PHP用字符串删除一行

时间:2014-02-17 14:59:33

标签: php sql

我正在尝试删除与传入方法的字符串匹配的行。

$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);

我尝试了一些SQL语句的组合,但无法让它工作

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

发现SQL错误:

username = username=?

应该是

username = ?