简体   繁体   English

MySQL,更新不在数组php中的位置

[英]Mysql, update where not in array php

I am trying to update with 0 the rows that is not in the array I get from the xml. 我试图用0更新不在xml中的行。

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

When I echo $A it prints it out correctly like this: '60','62','65','73','74','79','83','90','112','124' However it does not do the update, whats wrong? 当我回显$ A时,它会像这样正确打印:'60','62','65','73','74','79','83','90','112',' 124'但是它不执行更新,怎么了?

You should start by escaping your XML values to avoid SQL injection: 您应该先转义XML值以避免SQL注入:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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