2014-07-20 146 views
-1
<?php 
$db = new PDO('mysql:host=localhost;dbname=wordpress', 'root',''); 
//---------prepare 
$delete3 = $db->prepare("DELETE FROM wp_term_relationships WHERE object_id=:id"); 
$delete2 = $db->prepare("DELETE FROM wp_posts WHERE ID=:id"); 
$delete = $db->prepare("DELETE FROM wp_postmeta WHERE post_id=:id");; 
$select = $db->prepare("SELECT post_id FROM reference WHERE x_id=?"); 
$delete->bindParam(':id', $id, PDO::PARAM_STR); 
$delete2->bindParam(':id', $id, PDO::PARAM_STR); 
$delete3->bindParam(':id', $id, PDO::PARAM_STR); 
//---------- 
echo 'conected-----'; 
{$delfeed = 'LOTS OF NUMBERS';} 
$array = explode(',',$delfeed); 
foreach($array as $deadman){ 
    $select->execute(array($deadman)); 
    $row = $select->Fetch(PDO::FETCH_ASSOC); 
    $id = $row['post_id']; 
     if ($id){ 
     echo "$id"."\n"; 
     $delete->execute(); 
     $delete2->execute(); 
     $delete3->execute(); 
}} 
echo 'done!'; 
?> 

其簡單刪除腳本,但它不刪除,它打印正確的$ id的女巫意味着正在工作,直到有但刪除雲香蕉,雙重檢查表名,colums ... tryied與問號佔位工作insted的綁定參數,但沒有 更新的:它粘在陣列中的foreachPDO刪除不工作

回答

2

問題的第一個項目是,你設置$id變量是有史以來之前。

<?php 
$db = new PDO('mysql:host=localhost;dbname=wordpress', 'root',''); 

$delete3 = $db->prepare("DELETE FROM wp_term_relationships WHERE object_id=?"); 
$delete2 = $db->prepare("DELETE FROM wp_posts WHERE ID=?"); 
$delete = $db->prepare("DELETE FROM wp_postmeta WHERE post_id=?"); 
$select = $db->prepare("SELECT post_id FROM reference WHERE x_id=?"); 

foreach($array as $deadman){ 
    $select->execute(array($deadman)); 
    $row = $select->Fetch(PDO::FETCH_ASSOC); 
    $id = $row['post_id']; 
    if ($id) { 
     echo "$id"."\n"; 
     $data = array($id); 
     $delete->execute($data); 
     $delete2->execute($data); 
     $delete3->execute($data); 
    } 
    $id++; 
} 
echo 'done!'; 
?> 

我沒有更新你的代碼一點點,因爲一些位沒有意義,如$delfeed是一個空字符串,增加,即使它被覆蓋了$id變量,且放置隨意左右大括號。但正如你所看到的,在if($id)的檢查中,我將變量綁定在那裏。

除了使用我的方法,您可以指定文檔頂部的變量$id爲值0;然後,在完成所有準備說明之後,您可以使用功能bindParam而不是bindValue

$id = 0; 

// Prepare Statements 

$delete->bindParam(':id', $id, PDO::PARAM_STR); 
$delete2->bindParam(':id', $id, PDO::PARAM_STR); 
$delete3->bindParam(':id', $id, PDO::PARAM_STR); 

// Rest of Original Code With No Changes 

這部作品的原因,是因爲bindParam經過在查詢執行時間參考。見PHP bindParam Document以及this answer.

編輯:解決方案的2個固定的foreach問題

<?php 
$db = new PDO('mysql:host=localhost;dbname=wordpress', 'root',''); 

$delete3 = $db->prepare("DELETE FROM wp_term_relationships WHERE object_id=:id"); 
$delete2 = $db->prepare("DELETE FROM wp_posts WHERE ID=:id"); 

// Had a double semicolon trailing the function. Removed one of them 
$delete = $db->prepare("DELETE FROM wp_postmeta WHERE post_id=:id"); 
$select = $db->prepare("SELECT post_id FROM reference WHERE x_id=?"); 

$id = 0; // Define Variable BEFORE bindParam 

$delete->bindParam(':id', $id, PDO::PARAM_STR); 
$delete2->bindParam(':id', $id, PDO::PARAM_STR); 
$delete3->bindParam(':id', $id, PDO::PARAM_STR); 


$delfeed = ''; // Why Have Curly Braces around this? 

$array = explode(',',$delfeed); 

foreach($array as $deadman) { 
    $select->execute(array($deadman)); 
    $row = $select->Fetch(PDO::FETCH_ASSOC); 
    $id = $row['post_id']; 
    if ($id){ 
     echo "$id"."\n"; 
     $delete->execute(); 
     $delete2->execute(); 
     $delete3->execute(); 
    } 
} 
echo 'done!'; 
?> 
+0

facepalmed自己的辛苦,它掠過我的臉 - 」謝謝哥們 – user3610512

+0

如果你還沒有,檢查的最底部我添加了關於使用'bindParam'的帖子。它可能會使代碼更容易更新。 – David

+0

不錯!但現在它陷入了foreach,$ delfeed充滿了數字我只是把它們拿出來,所以代碼不會太長 – user3610512