elseif mysql查询问题

发布于 2024-11-26 17:32:09 字数 1064 浏览 1 评论 0原文

我在使用 elseif MySQL 查询来搜索数据库时遇到了一些麻烦,具体取决于表单中的选择。

它没有返回错误,但没有返回应有的结果,所以我假设第二个 elseif 查询根本没有运行。第一个有效(当然,当做出正确的选择时)。

谁能指出我做错了什么?

<?php
$jobtype = $_POST['jobtype'];
$country = $_POST['countries'];
$city = $_POST['cities'];

if ($jobtype=="All Jobs" && (isset($_POST['countries'])) && $city !="Whole Region") 
    {
  $result = mysql_query(
  "SELECT *
   FROM jobinformation 
   WHERE country = '$country' AND county = '$city'
   ORDER BY job_id DESC");

   if (!$result) 
     { 
      die('Invalid query: ' . mysql_error());
     }

   $rows=mysql_num_rows($result);

   for ($j = 0; $j < $rows ; ++$j)
     {
    $row = mysql_fetch_row($result);
    echo 'jobtitle: ' . $row[3] .'<br />';
    echo 'Company: ' . $row[2] .'<br />';
     }
}

elseif ($jobtype=="All Jobs" && (isset($_POST['countries'])) && $jobtype=="Whole Region")

{

$result = mysql_query(
"SELECT *
FROM jobinformation
WHERE country = '$country'
ORDER BY job_id DESC");

etc etc (same as above)

}

i'm having a little trouble with an elseif MySQL query i'm using to search my database depending on what selections are made in a form.

It's not returning an error, but its not returning the results it should, so i'm assuming the second elseif query isn't running at all. The first one works (when the right selections are made of course).

Can anyone point out what i'm doing wrong?

<?php
$jobtype = $_POST['jobtype'];
$country = $_POST['countries'];
$city = $_POST['cities'];

if ($jobtype=="All Jobs" && (isset($_POST['countries'])) && $city !="Whole Region") 
    {
  $result = mysql_query(
  "SELECT *
   FROM jobinformation 
   WHERE country = '$country' AND county = '$city'
   ORDER BY job_id DESC");

   if (!$result) 
     { 
      die('Invalid query: ' . mysql_error());
     }

   $rows=mysql_num_rows($result);

   for ($j = 0; $j < $rows ; ++$j)
     {
    $row = mysql_fetch_row($result);
    echo 'jobtitle: ' . $row[3] .'<br />';
    echo 'Company: ' . $row[2] .'<br />';
     }
}

elseif ($jobtype=="All Jobs" && (isset($_POST['countries'])) && $jobtype=="Whole Region")

{

$result = mysql_query(
"SELECT *
FROM jobinformation
WHERE country = '$country'
ORDER BY job_id DESC");

etc etc (same as above)

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

咋地 2024-12-03 17:32:09

看起来像这样:

elseif ($jobtype=="All Jobs" && (isset($_POST['countries'])) && $jobtype=="Whole Region")

应该是这样:

elseif ($jobtype=="All Jobs" && (isset($_POST['countries'])) && $city=="Whole Region")

当您在 if 中对照 $city 检查时,您会在 else if 中检查 $jobtype

另外,如果 的前 2 部分if 语句是相同的,您不需要 elseif 这里您可以只使用 else

looks like this:

elseif ($jobtype=="All Jobs" && (isset($_POST['countries'])) && $jobtype=="Whole Region")

Should be this:

elseif ($jobtype=="All Jobs" && (isset($_POST['countries'])) && $city=="Whole Region")

You're check against $jobtype in the else if when you check it against $city in the if

Also if the first 2 parts of the if statement are the same you don't need an elseif here you can just use an else

氛圍 2024-12-03 17:32:09

尽管不知道您遇到了什么错误,但请允许我更正您的代码。

<?php
$jobtype = $_POST['jobtype'];
$country = $_POST['countries'];
$city = $_POST['cities'];

if ($jobtype=="All Jobs" && (isset($_POST['countries'])) && $city !="Whole Region") {
  $sql = "SELECT *
           FROM jobinformation 
          WHERE country = '" . mysql_real_escape_string($country) . "' AND county = '" . mysql_real_escape_string($city) . "'
          ORDER BY job_id DESC";
} elseif ($jobtype=="All Jobs" && (isset($_POST['countries'])) && $jobtype=="Whole Region") {
  $sql = "SELECT *
            FROM jobinformation
           WHERE country = '" . mysql_real_escape_string($country) . "'
           ORDER BY job_id DESC");
}

$result = mysql_query($sql) or die('Invalid query: ' . mysql_error());

while($row = mysql_fetch_assoc($result)) {
  echo "jobtitle: {$row['jobtitle']}<br />";
  echo "Company: {$row['company']<br />";
}

这是构建代码的更好方法。

Despite not knowing what error you are having, please allow me to correct your code.

<?php
$jobtype = $_POST['jobtype'];
$country = $_POST['countries'];
$city = $_POST['cities'];

if ($jobtype=="All Jobs" && (isset($_POST['countries'])) && $city !="Whole Region") {
  $sql = "SELECT *
           FROM jobinformation 
          WHERE country = '" . mysql_real_escape_string($country) . "' AND county = '" . mysql_real_escape_string($city) . "'
          ORDER BY job_id DESC";
} elseif ($jobtype=="All Jobs" && (isset($_POST['countries'])) && $jobtype=="Whole Region") {
  $sql = "SELECT *
            FROM jobinformation
           WHERE country = '" . mysql_real_escape_string($country) . "'
           ORDER BY job_id DESC");
}

$result = mysql_query($sql) or die('Invalid query: ' . mysql_error());

while($row = mysql_fetch_assoc($result)) {
  echo "jobtitle: {$row['jobtitle']}<br />";
  echo "Company: {$row['company']<br />";
}

This is a much better way to structure your code.

碍人泪离人颜 2024-12-03 17:32:09

在一个地方,您正在检查“整个地区”的 $jobtype,在另一个地方,您正在检查“整个地区”的 $city - 您是否混合了变量向上?

In one spot you're checking $jobtype for "Whole Region", in the other you're checking $city for "Whole Region" - have you gotten the variables mixed up?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文