2017-04-30 100 views
0

一直在努力, 我想用0更新不在我從xml獲得的數組中的行。從陣列xml更新mysql

$sus = array(); 
foreach($xml->property as $node) { 
$sus[] = $node->suid; 
} 
$A = "'".implode("','",$sus)."'"; 
echo $A; 
$sth = $dbh->prepare("UPDATE tabla SET 
alta = 0 
WHERE suid NOT IN ($A)"); 
$sth->execute($sus); 

當我回聲它正確打印出來這樣$ A: '60', '62', '65', '73', '74', '79', '83',」 90','112','124' 但是它沒有做更新, 什麼錯了? 在此先感謝

回答

0

你應該逃脫你的XML值,以避免SQL注入啓動:

$escapedValues = str_repeat('?,', count($sus) - 1) . '?'; 
$sth = $db->prepare("UPDATE tabla SET alta = 0 WHERE suid NOT IN ($escapedValues)" 
$sth->execute($sus); 
+0

這一工作的感謝,我以前不知道逃逸需要的地方上PDO的更新。 查詢中存在名稱錯誤。 這裏是完整的代碼,以防其對任何人有用: $ sus = array(); ($ xml-> property as $ node){ $ sus [] = $ node-> suid; } $ escapedValues = str_repeat('?,',count($ sus) - 1)。 '?'; $ sth = $ dbh-> prepare(「UPDATE tabla SET alta = 0 WHERE suid NOT IN($ escapedValues)」); $ sth-> execute($ sus); – Helenp

+0

太棒了!很高興爲您服務。剛剛編輯我的答案與適當的變量名稱:) – Clorichel