我有以下代码,类似于带有多个输入的搜索引擎形式,其中结果有点像是字符数等(完美匹配)
// 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; }
它现在正在运作,但是任何人都可以确认将来会好吗?
答案 0 :(得分:1)
来自SQL:
$ sql =“SELECT * FROM jobs WHERE”。 (!空($ wheres)?implode(“AND”,$ wheres):'1 = 1')。“;”;
只需添加特殊变量:
$ special = $ _POST ['special']; //这将从文本框中获取数据
然后将其添加到sql语句
$ sql =“SELECT * FROM Jobs where Where $ special'aaaa%'AND”。 (!空($ wheres)?implode(“AND”,$ wheres):'1 = 1')。“;”;
答案 1 :(得分:0)
重写避免变量变量名,并使用mysql_real_escape_string(尽管你应该使用mysqli或pdo): -
<?php
// 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('1=1');
// 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
if (isset($_POST[$field]) && trim($_POST[$field])!='')
{
$wheres[]="`$field`='".mysql_real_escape_string(trim($_POST[$field]))."'";
}
}
if (isset($_POST['special']) && trim($_POST['special'])!='')
{
$wheres[] = " case_reference' LIKE '".mysql_real_escape_string(trim($_POST['special']))."%'";
)
// build SELECT statement from WHERE clauses
$sql="SELECT * FROM jobs WHERE (".implode(" AND ",$wheres).") ;";
?>