2016-11-23 47 views
0

我有以下要求。我有四个字段公司名称,经验, 薪水从,薪水到。现在,我希望用户输入他想要的任何字段,并根据用户的输入触发SELECT查询,例如,如果用户仅输入Experience和Salary From条目,则结果将显示与输入的体验相匹配的所有记录,薪资范围大于入职薪酬。当条件不固定时,SELECT查询的数量是多少?

if(isset($_GET["btnSubmit"])){ 

$conn = mysqli_connect("localhost","root","","jobportal"); 

$company = $_GET['txtCompanyName']; 
$experience = $_GET["txtExperience"]; 
$salaryFrom = $_GET["txtSalaryFrom"]; 
$salaryTo = $_GET["txtSalaryTo"]; 

//$sql = ? 
$stmt = $conn->query($sql); 

$raw_results = $stmt->fetch_array(); 
if($raw_results[0] > 0){ 

    //$sql=? 
    $stmt = $conn->query($sql); 
    while($results = $stmt->fetch_array()){ 
     echo "<p><h3>".$results[0]."</h3>".$results[1]."</p>"; 
    } 

} 
else{ // if there is no matching rows do following 
    echo "No results"; 
} 


<form action="" method="get"> 
        <p> 
         <label for="companyname" class="icon-user">  Company Name 
          <span class="required">*</span> 
         </label> 
         <input type="text" name="txtCompanyName" placeholder="Company Name" /> 
        </p> 

        <p> 
         <label for="experience" class="icon-pencil"> Experience 
          <span class="required">*</span> 
         </label> 
         <input type="text" name="txtExperience" placeholder="Experience"/> 
        </p> 

        <p> 
         <label for="salaryfrom" class="icon-pencil"> Salary From 
          <span class="required">*</span> 
         </label> 
         <input type="text" name="txtSalaryFrom" placeholder="Salary Starting Range"/> 
        </p> 

        <p> 
         <label for="salaryto" class="icon-pencil"> Salary To 
          <span class="required">*</span> 
         </label> 
         <input type="text" name="txtSalaryTo" placeholder="Salary End Range"/> 
        </p> 
        <p> 
         <input type="submit" value="Submit" name="btnSubmit"/> 
        </p> 
       </form> 
+0

:其中(thefield = user_argument或user_argument为空)或(...) –

+1

而且..你能告诉我们你的PHP代码吗?你应该只使用IF – Aks

+0

@Aks输入php代码。 – phpNoob

回答

1

可以动态立足岗位

$form = $_GET; 

    $where = "1=1 "; 

    // Check the second input 
    if(isset($form["txtSalaryFrom"]) and is_numeric($form["txtSalaryFrom"])) { 
     $where. = "and u.salary >= :txtSalaryFrom"; 
    } 
    // Check the first input 
    if(isset($form["txtSalaryTo"]) and is_numeric($form["txtSalaryTo"])) { 
     $where. = "and u.salary < :txtSalaryTo "; 
    } 

    if(isset($form["txtCompanyName"])) { 
    $where. = "and u.txtCompanyName like :txtCompanyName"; 
    } 

    // ETC 

    // Create the prepared query 
    $stmt = $dbh->prepare("SELECT * FROM Users as u WHERE $where"); 


    if(isset($form["txtSalaryFrom"]) and is_numeric($form["txtSalaryFrom"])) { 
     $stmt->bindParam(':txtSalaryFrom', $form["txtSalaryFrom"]); 
    } 

    if(isset($form["txtSalaryTo"]) and is_numeric($form["txtSalaryTo"])) { 
     $stmt->bindParam(':txtSalaryTo', $form["txtSalaryTo"]); 
    } 

    if(isset($form["txtCompanyName"])) { 
     $stmt->bindParam(':txtCompanyName', %.$form["txtSalaryTo"].%); 
    }  
    $stmt->execute(); 
+1

当我喜欢'%:company%'时,我将如何为LIKE'%company%'执行此操作。 **你的SQL语法有错误;如果(isset($ form [“txtCompanyName”])检查与您的MariaDB服务器版本相对应的手册,以找到在第1行的'company_name LIKE'%:company%''附近使用的正确语法** – phpNoob

+0

){ $ stmt-> bindParam(':txtCompanyName',%。$ form [“txtSalaryTo”]。%); } – ASFolken

+0

我正在做,但由于某种原因,它仍然给这个错误,你能帮我解决它。如果(!empty($ company))\t {$ where = $ where。“AND company_name LIKE:company”;} if(!empty($ company))\t {$ companyFormat =“'%”。$ company。“% '“; $ stmt-> bind_param(”:company“,$ companyFormat);} – phpNoob

1

像这样的事情

<?php 

$company = $_GET['txtCompanyName']; 
$experience = $_GET["txtExperience"]; 
$salaryFrom = $_GET["txtSalaryFrom"]; 
$salaryTo = $_GET["txtSalaryTo"]; 


$sql = 'SELECT * FROM tablename WHERE 1 = 1 '; 


if(!empty($company)){ 
    $sql .= " AND tablename.CompanyName LIKE %$company% "; 
} 

if(!empty($experience)){ 
    $sql .= " AND tablename.Experience = $experience "; 
} 
if(!empty($salaryFrom)){ 
    $sql .= " AND tablename.Salary > $salaryFrom "; 
} 
if(!empty($salaryTo)){ 
    $sql .= " AND tablename.Salary < $salaryTo "; 
} 

而且虽然,你应该确保你防范SQL注入。也许使用PDO和准备http://php.net/manual/en/pdo.prepare.php

+0

通过准备好的语句执行所有输入时,我收到以下错误**您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,以获得在第1行':company'附近使用的正确语法** – phpNoob

+0

首先尝试不使用pdo。只是为了看看你能否使查询工作。 –