elseif mysql query problem
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")
{
$re开发者_如何学编程sult = mysql_query(
"SELECT *
FROM jobinformation
WHERE country = '$country'
ORDER BY job_id DESC");
etc etc (same as above)
}
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
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.
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?
精彩评论