elseif mysql查询问题
我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来像这样:
应该是这样:
当您在 if 中对照
$city
检查时,您会在 else if 中检查$jobtype
另外,如果 的前 2 部分if 语句是相同的,您不需要
elseif
这里您可以只使用 elselooks like this:
Should be this:
You're check against
$jobtype
in the else if when you check it against$city
in the ifAlso 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尽管不知道您遇到了什么错误,但请允许我更正您的代码。
这是构建代码的更好方法。
Despite not knowing what error you are having, please allow me to correct your code.
This is a much better way to structure your code.
在一个地方,您正在检查“整个地区”的
$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?