1

我有一个搜索页面 tat 允许用户使用按省、区或村搜索来搜索

如果用户按省选择浏览器显示正确答案

但是如果用户选择省和地区,系统会改变它显示相同的结果

  • 省ID
  • 省名区
  • 区号
  • 区名
  • 省ID

村庄

  • ID
  • 村名
  • 区号

成员 user_id

  • 省ID
  • 区号
  • 村庄编号

我希望当用户选择一种类型或所有系统时必须显示与选择相关的用户列表而不是所有用户

代码:

    $errorMSG = "";
    $outputlist = "";
    //**********search by locationn***************************************//
    if(isset($_POST['listbyq']))
    {    
    //********************by governorate**************************************//
       if($_POST['listbyq']=="by_gov")
       {
           $bygov = $_POST['governorate'];
           @ $bydist = $_POST['district'];
           @ $byvillage = $_POST['village'];
            echo $bygov;
            echo $bydist;
            echo $byvillage;


$sql = mysql_query("SELECT user_id,first_name, last_name, birth_date, registered_date, 
s.specialization_name,
g.governorate_name,
d.district_name,
v.village_name 
      FROM members u
                   INNER JOIN  specialization s 
                    ON u.specialization = s.specialization_id
                    INNER JOIN governorate g
                    ON u.governorate = g.governorate_id
                    INNER JOIN districts d
                    ON u.district = d.district_id
                    INNER JOIN village v
                    ON u.village = v.id
                   where ($bygov = '' or governorate = '$bygov') and
                         ($bydist = '' or district = '$bydist') and
                         ($byvillage = '' or village = '$byvillage')")
                         or die(mysql_error());
           $num_row = mysql_num_rows($sql);
           if($num_row > 0 )
           {
               while($row = mysql_fetch_array($sql))
               {
                  $row_id = $row['user_id'];
                  $row_first_name =  $row['first_name'];
                  $row_last_name =  $row['last_name'];
                  $row_birthdate =  $row['birth_date'];
                  $row_registered_date = $row['registered_date'];
                  $row_spec = $row['specialization_name'];
                  $row_gov = $row['governorate_name'];
                  $row_dist = $row['district_name'];
                  $row_village = $row['village_name'];

                    ////***********for the upload image*************************//
             $check_pic="members/$row_id/image01.jpg";
             $default_pic="members/0/image01.jpg";
             if(file_exists($check_pic))
             {
                 $user_pic="<img src=\"$check_pic\"width=\"120px\"/>";
             }
             else
             {
                 $user_pic="<img src=\"$default_pic\"width=\"120px\"/>";
             }

              $outputlist.='
         <table width="100%">
                     <tr>
                        <td width="23%" rowspan="5"><div style="height:120px;overflow:hidden;"><a href =              "http://localhost/newadamKhoury/profile.php?user_id='.$row_id.'" target="_blank">'.$user_pic.'</a></div></td>
                        <td width="14%"><div  align="right">Name:</div></td>
                        <td width="63%"><a href = "http://localhost/newadamKhoury/profile.php?user_id='.$row_id.'" target="_blank">'.$row_first_name.' '.$row_last_name.'</a></td>
                        </tr>

                        <tr>
                          <td><div align="right">Birth date:</div></td>
                          <td>'.$row_birthdate.'</td>
                        </tr>
                        <tr>
                         <td><div align="right">Registered:</div></td>
                         <td>'.$row_registered_date.'</td>
                        </tr>

                        <tr>
                         <td><div align="right">Job:</div></td>
                         <td>'.$row_spec.'</td>
                        </tr>

                        <tr>
                         <td><div align="right">Location:</div></td>
                         <td>'.$row_gov.'__'.$row_dist.'__'.$row_village.'</td>
                        </tr>
                        </table>
                        <hr />
                ';

               }
           }

       }
       else
       {
           $errorMSG = "No member within this selected governorate";
       }
    }
4

1 回答 1

0

问题是你的where条款:

   WHERE governorate = '$bygov' OR district = '$bydist' OR village = '$byvillage'"

这些是通过ornot连接的and

据推测,当有人选择省和区时,您想要的是区省,而不是区省。一种方法是将where子句编码为:

where ($bygov = '' or governorate = '$bygov') and
      ($bydist = '' or district = '$bydist') and
      ($byvillage = '' or village = '$byvillage')

另一种方法是在应用程序级别使用自定义代码where为每种情况自定义子句,例如:

$where = '';
if ($bygov != '') {$where = $where." governate = '$bygov' and "}
if ($bydist != '') {$where = $where." district = '$bydist' and"}
if ($byvillage != '') {$where = $where." village = '$byvillage' and"}
$where = $where." 1=1"
于 2013-05-19T15:20:15.193 回答