gpt4 book ai didi

php - 我如何在下面的代码中进行 LIKE 搜索?

转载 作者:行者123 更新时间:2023-11-29 13:25:12 24 4
gpt4 key购买 nike

我有以下代码,类似于具有多个输入的表单搜索引擎,其中结果在字符数等方面有点绝对(完美匹配).

// build array of field names=============================================================================
$fields=array('user','customer','vessel','country',
'port','eta','service_station','type_of_service',
'case_reference','status');

// initialize empty array for WHERE clauses
$wheres=array();

// loop through field names, get POSTed values,
// and build array of WHERE clauses, excluding false values
foreach ($fields as $field) {
// get existing field value from POST, mark missing or empty value as FALSE
${$field} = isset($_POST[$field]) && trim($_POST[$field])!=''
? trim($_POST[$field]) : false;

// add to array of WHERE clauses only if value is not FALSE
if (${$field}) { $wheres[]="$field='".${$field}."'"; }

}

// build SELECT statement from WHERE clauses
$sql="SELECT * FROM jobs WHERE ".
(!empty($wheres) ? implode(" AND ",$wheres) : '1=1').
";";

我想要做的是在表单中添加输入

<label for="special">Special Search</label>
<input type="text" name="special" id="special_search">

用户可以在case_reference字段中搜索并获取与前四个字符匹配的结果。另外,就 AND 或 OR 以及 TRUE 或 FALSE 语句而言,我希望这个新输入与其他输入一样工作。

感谢所有帮助,提前谢谢:)

更新:我没有重写整个内容,而是在上一篇文章的开头提出了以下代码:

$joker = $_POST['special'];
$joker1 = substr($joker1, 0, 4);
if(isset($_POST['case_reference']) && !empty($_POST['case_reference'])
&& empty($_POST['special'])) {

} else { $_POST['case_reference'] = $joker1; }

它现在可以工作,但任何人都可以确认它将来会没问题吗?

最佳答案

来自 SQL:

$sql="SELECT * FROM jobs WHERE ". (!empty($wheres) ? implode(" AND ",$wheres) : '1=1').";";

只需添加一个特殊变量:

$special = $_POST['special']; // this will get the data from the textbox

然后将其添加到sql语句中

$sql="SELECT * FROM jobs WHERE LIKE $special 'aaaa%' AND ". (!empty($wheres) ? implode(" AND ",$wheres) : '1=1').";";

关于php - 我如何在下面的代码中进行 LIKE 搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20242370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com