2016-11-23 12 views
0

私には次の要件があります。私は4つのフィールドを持っています会社名、経験、 給料から、給料を。ここで、ユーザーが希望するフィールドを入力し、ユーザーの入力に基づいてSELECTクエリが実行されるようにします。たとえば、ユーザーがExperience and 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がnullの場合)または(...) –

+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構文に誤りがあります。 1行目の 'company_name LIKE'%:company% '' at line 1 ''の近くで使用する正しい構文については、MariaDBサーバーのバージョンに対応するマニュアルを参照してください。** – phpNoob

+0

if(isset($ form ["txtCompanyName"]) ){ $ stmt-> bindParam( ':txtCompanyName'、%。$ form ["txtSalaryTo"]。%); } – ASFolken

+0

私は正確にやっていますが、何らかの理由でまだこのエラーが出ています。 \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を先に試してみてください。クエリが機能するかどうかを確認するだけです。 –

0

あなたが見撮りたいことがあり、ポストに基づいて、SQLクエリを作成することができます準備:通常plinqlinqforphpphplinq

関連する問題