這裏是我的問題,我有2個表這裏涉及到:TINVOICES和TJOBS動態搜索選項2個表
TINVOICES - Job_no
TINVOICES - Year
TJOBS - Staff
TINVOICES - Month
TJOBS - Country
TINVOICES - Paid
TJOBS - Client_no
TINVOICES - 30 Days
我想要的搜索選項是動態的,像它應該接受搜索工作,今年,獨自一人。或者僅支付或僅僅一個月...等等。我有兩張桌子。 你能幫我解決一下我的方法嗎?
查看我的代碼如下。
太謝謝你了!
這裏是我的搜索選項:
if (isset($_POST['job_no'])) {
$job_no = $_POST['job_no'];
if ($_POST['job_no'] != "") { $job_no = " job_no=".$job_no." AND"; $search += 1;}
}
..other ifs
if ($search != 0) {
if (isset($_POST['country'])) {
if ($_POST['country'] == "") {
$query = "WHERE $job_no$month$id$year$staff_no$paid";
}
else {
$query = "AND $job_no$month$id$year$staff_no$paid";
}
}
else{
$query = "WHERE $job_no$month$id$year$staff_no$paid";
}
}
//Remove string AND on the end of a query if found
if (substr($query, -3) == "AND") { $query = substr($query, 0, -3); }
if (isset($_POST['country']) && $_POST['country'] != "") {
$country = $_POST['country'];
if ($country == "All") {
$sql = "SELECT p . * FROM tinvoices $query AS p ";
}
else if ($country == "Middle East") {
$sql = "
SELECT p . *
FROM tinvoices AS p
LEFT JOIN tjobs AS j
ON p.job_no = j.job_no
WHERE j.location = 'Qatar' OR j.location = 'UAE' $query ";
}
else {
$sql = "
SELECT p . *
FROM tinvoices AS p
LEFT JOIN tjobs AS j
ON p.job_no = j.job_no
WHERE j.location = '$country' $query ";
}
$result = mysqli_query($con,$sql) or die("MySQL Error: ".mysql_error());
}
elseif (isset($_POST['client_no']) && $_POST['client_no'] != "") {
$client_no = $_POST['client_no'];
$sql = "
SELECT p . *
FROM tinvoices AS p
LEFT JOIN tjobs AS j
ON p.job_no = j.job_no
WHERE j.location = '$country' $query ";
echo $sql;
}
else {
$sql = "SELECT p . * FROM tinvoices AS p $query";
//echo $sql;
$result = mysqli_query($con,$sql) or die("MySQL Error: ".mysql_error());
}
動態以什麼方式?如果你用一種我們能夠理解你想要做什麼,你試圖做什麼,什麼不起作用的方式來制定你的問題,那將是非常好的 – nrathaus
嗨,感謝您的評論。我希望搜索選項是動態的,就像它應該接受搜索工作和年份一樣。或者僅支付或僅僅一個月...等等。我有兩張桌子。 – EatCodePlaySleep
你已經在正確的軌道上,只是分割「WHERE」部分,並保持查詢的第一部分爲一個,這樣$ sql =「select ..」,然後添加$ sql。=「其中.. 「 – nrathaus