2014-02-17 174 views
1

我想刪除一個匹配傳入方法的字符串的行。PHP刪除一個字符串的行

$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語句的幾個組合,但不能得到一個工作

回答

0

現貨SQL錯誤:

username = username=?

應該

username = ?

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