php - 在 HTML 搜索表单的 1 个输入文本中搜索超过 1 个值

标签 php mysql sql mysqli

我有一个使用 HTML 的常用搜索表单,它将从 mysql 中提取数据(连接使用 PHP 和 mysqli)。该函数在搜索 1 个值时效果很好,但我想让用户可以搜索 1 个以上的值,这些值将以逗号 (,) 分隔。

例如: Searching 1 value works well 搜索 1 个值效果很好。

但我想在搜索框中搜索 2 个或更多值,例如:42-7278954,53-1217544,07-2517487,...

我希望用户可以输入如下图所示的内容,结果将有 2 行 --> CN_no 42-727895453-1217544:

enter image description here

我目前的查询是:

$sql = "SELECT * FROM mock_data WHERE CN_no IN ('{$CN_no}') OR doc_no IN ('{$doc_no}')";

注意:CN_no 是“发货代码”,doc_no 是“引用号”

但是...很明显,由于语法不正确,它会给我一个错误。

请帮我修改一下。谢谢。

======== 根据 vp_arth 的回答更新查询 ========

$cn = explode(',', $CN_no);
$incn = str_repeat('?, ', count($cn)-1).'?';
$doc = explode(',', $doc_no);
$indoc = str_repeat('?, ', count($doc)-1).'?';

$query = "SELECT * FROM mock_data WHERE CN_no IN ({$incn}) or doc_no IN ({$indoc})";
$result = $conn->query($query , array_merge($incn, $indoc));

但它给了我一个 error

最佳答案

这是在 mysqli 中使用带有动态输入的 prepared statements 的可能解决方案。 parameter binding 的类型是静态的,在本例中参数是 strings

/**
 * connecting to the database
 * defining in how manye columns you want to search (important to create the correct amount of arguments)
 */
$conn = new mysqli('localhost', 'root', '', 'test');
$columnsToSearch = 2;

/**
 * the numbers you want to search delimited by ","
 */
$CN_no = '42-7278954,53-1217544,07-2517487';
$cn = explode(',', $CN_no);

/**
 * writing the numbers to search into variables
 * putting the references of those variables into an array
 * 
 * the references will be used as arguments for the prepared statement
 */
$values = array();
for ($i = 0; $i < $columnsToSearch; $i++) {
    foreach ($cn as $k => $value) {
        $temp{$k}{$i} = $value;
        $values[] = &$temp{$k}{$i};
    }
}
/**
 * putting together the "types"-part for the binding of the prepared statement
 */
$types = array(str_repeat('s', count($cn) * $columnsToSearch - 1) . 's');
/**
 * merging types and references
 */
$argumentsArray = array_merge($types, $values);
/**
 * creating placeholder string for the query
 */
$placeholder = str_repeat('?, ', count($cn) - 1) . '?';

$stmt = $conn->prepare('SELECT CN_no, doc_no FROM mock_data WHERE CN_no IN (' . $placeholder . ') or doc_no IN (' . $placeholder . ')');

/**
 * check http://us3.php.net/manual/en/mysqli-stmt.bind-param.php#104073 to read what is happening here
 */
$ref = new ReflectionClass('mysqli_stmt');
$method = $ref->getMethod("bind_param");
$method->invokeArgs($stmt, $argumentsArray); 

$stmt->execute();

/**
 * fetching the result
 */
$stmt->bind_result($CN_no, $doc_no);
$row_set = array();
while ($row = $stmt->fetch()) {
    $row_set[] = array('CN_no' => $CN_no, 'doc_no' => $doc_no);
}
var_dump($row_set);
exit;

我调整了来自http://us3.php.net/manual/en/mysqli-stmt.bind-param.php#104073的评论所以它适合您的场景。

顺便说一句。使用 PDO 作为数据库 API,这将更容易编写和阅读。稍后我可能会为 PDO 添加一个示例。

关于php - 在 HTML 搜索表单的 1 个输入文本中搜索超过 1 个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41392128/

相关文章:

mysql - 如何避免在 sql SELECT 和 WHERE 中出现相同的表达式?

MySQL - 在 "normal"连接不可能时显示/计数连接记录

mysql - 选择每行及其列索引的最大值

php - 如何修复 PHP 中的 'Creating default object from empty value' 警告

php - 将表单结果发送到另一个页面 php mysql

mysql - 如何在不重启 MySQL 的情况下刷新 performance_schema 统计信息?

php - 根据所选语言订购记录

mysql - MySQL 过程中出现错误 1064

php - 获取所有表单元素和不规则字符

php - 在网络服务器上处理上传的文件而不先在本地存储?