【问题标题】:Multiple selection in a single table using php mysql使用php mysql在单个表中进行多项选择
【发布时间】:2012-09-17 19:00:19
【问题描述】:

我正在尝试从单个表中提取多行。我正在尝试使用不同邮政编码的所有男性或所有女性。

<?php
$zipCodes = array("55555", "66666", "77777", etc...);

$fetchUser = mysql_query("select * from users where gender = '$_POST[gender]' ".implode(" or zipCode = ", $zipCodes)." order by id desc");
while($var = mysql_fetch_array($fetchUser)) {
  code...
}
?>

【问题讨论】:

  • 我想你忘了问一个问题。你的代码有什么问题?
  • 欢迎来到stackoverflow..请解释您遇到的问题..您的帖子中没有问题。

标签: php mysql arrays


【解决方案1】:
// Prevent SQL injection for user input
$fetchUser = mysql_query("select * from users where gender = '".filter_var($_POST[gender], FILTER_SANITIZE_STRING)."' OR zipCode IN (".implode(",", $zipCodes).") order by id desc");)

【讨论】:

    【解决方案2】:

    您应该在此使用IN

    SELECT ...
    FROM   tableName
    WHERE gender = '$_POST[gender]' AND
          zipCode IN (55555, 6666, 77777)
    

    目前您的代码容易受到 SQL 注入的攻击。请阅读 PDOMySQLI 扩展。

    Read more on this article: Best way to prevent SQL injection in PHP
    PHP PDO: Can I bind an array to an IN() condition?

    【讨论】:

      猜你喜欢
      • 2018-12-25
      • 1970-01-01
      • 2011-11-05
      • 1970-01-01
      • 2011-02-26
      • 2014-04-14
      • 1970-01-01
      • 1970-01-01
      • 2017-12-20
      相关资源
      最近更新 更多