我有以下要求。我有四个领域公司名称,经验, 薪水来自,薪水。现在我希望用户输入他想要的任何字段,并根据用户的输入触发SELECT查询,例如,如果用户只输入Experience和Salary From条目,那么结果将显示所有与输入的体验相匹配的记录薪资范围大于来自的工资。
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 :(得分: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
答案 1 :(得分:1)
您可以根据帖子
动态创建SQL查询 $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();
答案 2 :(得分:0)
您可能需要查看:plinq linqforphp phplinq