这是我正在尝试做的事情:更新任意数量的行,将多个字段更改为SAME值.
$var = "1 3 5 7 9"; $query = "UPDATE tablename SET seen_before = 1 WHERE id = SET ($var);"
我的问题是:当我查看一个集合时,Where子句的确切语法是什么,以及如何通过php变量传递Set项目.
谢谢,
迈克尔
$ids = "1, 3, 5, 7, 9"; $query = "UPDATE tablename SET seen_before = 1 WHERE id IN ($ids)";
如果您的ID是动态的,并且它们可能是动态的,请使用类似的东西来正确地转义它们以获取SQL查询:
// Assuming $ids it's coming from an untrusted source, like $_GET $ids = array(1, 3, 5, 7, 9); $ids = array_map('intval', $ids); $ids = implode(', ', $ids); $query = "UPDATE tablename SET seen_before = 1 WHERE id IN ($ids)";