具有多个输入的PHP搜索表单

时间:2015-09-25 01:32:20

标签: php mysql

我有一个PHP脚本,我试图用来从db生成搜索结果,有多个搜索文本输入和提交。三个输入是术语,关键字和位置。我已经实现了搜索,但验证逻辑我发现有点困难。我将查询分为三个部分,以便当其中一个输入为空时,它会将查询的一部分添加到完整查询中。但是由于OR运算符在第一个输入为空时其余部分失败。

第二只眼睛在这里会有所帮助。请不要将此问题标记为过于通用只是查看查询,您将看到问题。

$term = mysqli_real_escape_string ($con , $_GET['term'] );
$location = mysqli_real_escape_string ($con , $_GET['location'] );
$keyword =  mysqli_real_escape_string ($con , $_GET['keyword'] );

if(empty($term)) {
$term1 = "";
}else{$term1 = "job_title LIKE '%".$term."%'";}

if(empty($location)) {
$loc1 = "";
}else{$loc1 = "location LIKE '%".$location."%'";}

if(empty($keyword)) {
$key1 = "";
}else{$key1 = "OR description LIKE '%".$keyword."%'";}


$sql = "SELECT * FROM jobs WHERE ".$term1." ".$loc1." ".$key1." ";
$r_query = mysqli_query($con,$sql);


while ($joblist = mysqli_fetch_array($r_query)){
$now = date('Y-m-d',time());

3 个答案:

答案 0 :(得分:2)

在第一个喜欢的声明之后,你错过了if(empty($location)) { $loc1 = ""; }else{$loc1 = "location LIKE '%".$location."%'";} ,这里 -

OR

您可能希望在location like之前放置"OR location LIKE '%".$location."%'" ,所以 -

RelativeLayout rLayout = (RelativeLayout) findViewById (R.id.rLayout);
Resources res = getResources(); //resource handle
Drawable drawable = res.getDrawable(R.drawable.newImage); //new Image that was added to the res folder
rLayout.setBackground(drawable);

答案 1 :(得分:1)

这是修改后的方法。尝试一下,如果它有效或不能回复。

if (!empty($_GET['term'])) {
    $where[] = " job_title like ? ";
    $params[] = '%' . $_GET['term'] . '%';
}
if (!empty($_GET['location'])) {
    $where[] = " location like ? ";
    $params[] = '%' . $_GET['location'] . '%';
}
if (!empty($_GET['keyword'])) {
    $where[] = " description like ? ";
    $params[] = '%' . $_GET['keyword'] . '%';
}
$sql_where = !empty($where) ? ' where ' . implode(' or ', $where) : '';
$query = "SELECT * FROM jobs $sql_where";
if (!($tot = mysqli_prepare($con, $query))) {
   echo "Prepare failed: (" . mysqli_errno($con) . ") " . mysqli_error($con);
} else {
    if(!empty($params)) {
    $params = array_merge(array($tot),
        array(str_repeat('s', count($params))), 
        $params);
    call_user_func_array('mysqli_stmt_bind_param', $params);
    // adapated from http://stackoverflow.com/questions/793471/use-one-bind-param-with-variable-number-of-input-vars and http://www.pontikis.net/blog/dynamically-bind_param-array-mysqli may need to be altered
    }
    mysqli_execute($tot);
}

答案 2 :(得分:0)

这似乎是基于@ chris85的建议。我不太确定$ params []如何发挥作用。任何解释都会很好。

<w:pPr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:pStyle w:val=\"PlainText\" /><w:numPr><w:ilvl w:val=\"0\" /><w:numId w:val=\"17\" /></w:numPr><w:rPr><w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" /><w:b /></w:rPr></w:pPr><w:r w:rsidRPr=\"000558F8\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rPr><w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" /></w:rPr><w:t>Should we use the term “Verify” instead of “Confirm”</w:t></w:r><w:r w:rsidRPr=\"000558F8\" w:rsidR=\"00F5335C\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rPr><w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" /></w:rPr><w:t xml:space=\"preserve\"> as per work instruction</w:t></w:r><w:r w:rsidRPr=\"000558F8\" w:rsidR=\"00411638\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rPr><w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" /></w:rPr><w:t>?</w:t></w:r><w:r w:rsidR=\"000558F8\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rPr><w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" /></w:rPr><w:br /><w:t>Med</w:t></w:r><w:r w:rsidRPr=\"000558F8\" w:rsidR=\"003E76BD\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rPr><w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" /><w:b /></w:rPr><w:br /><w:t xml:space=\"preserve\">JD: </w:t></w:r><w:r w:rsidRPr=\"000558F8\" w:rsidR=\"00A118AB\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rPr><w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" /><w:b /></w:rPr><w:t>Done.</w:t></w:r>