【问题标题】:Mysql, update where not in array phpMysql,更新不在数组php中的位置
【发布时间】:2017-04-30 17:33:43
【问题描述】:

我正在尝试用 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);

当我回显 $A 时,它会像这样正确打印出来: '60','62','65','73','74','79','83','90','112','124' 但是它不进行更新, 怎么了?

【问题讨论】:

    标签: php mysql arrays xml prepared-statement


    【解决方案1】:

    您应该首先转义您的 XML 值以避免 SQL 注入:

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

    【讨论】:

    • 这很有效,谢谢,不知道 pdo 更新需要在哪里转义。您的查询中的名称存在错误。这是完整的代码,以防它对任何人有用: $sus = array(); foreach( $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->执行($sus);
    • 太棒了!很高兴你。刚刚用适当的变量名编辑了我的答案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    相关资源
    最近更新 更多