2013-05-18 89 views
1

我有一個包含從數據庫中檢索到的值從3個不同的表複製在結果使用PHP MySQL和內部連接

省三個丟棄列表:

  • governosrate_id
  • governorate_name

區:

  • district_id
  • district_name

村:

  • ID
  • village_name

memebrs:

  • USER_ID
  • USER_NAME
  • 村 我想是當用戶選擇三個一個或全部下拉列表,系統必須顯示都涉及到選擇的一個結果

但問題是,當用戶從省選擇它工作正常但是當它選擇省,區複製它的結果與所選擇的值,如果用戶選擇省d istrict和village結果變成三倍

任何人都可以幫助我????

我將展示三種

,如果有人要投訴儘量不要打我

代碼的所有代碼:由

按省份

//**********search by locationn***************************************// 
if(isset($_POST['listbyq'])) 
{  
//********************by governorate**************************************// 
    if($_POST['listbyq']=="by_gov") 
    { 
     $bygov = $_POST['governorate']; 
     $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 governorate = '$bygov'")or die(mysql_error("Error: querying the governorate")); 

     $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"; 
    } 

if($_POST['listbyqa']=="by_dist") 
    { 
     @ $bydist = $_POST['district']; 
     $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 district = '$bydist'")or die(mysql_error("Error: querying the district")); 

     $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 District"; 
    } 

由村

if($_POST['listbyqb']=="by_city") 
    { 
     echo $_POST['listbyqb']; 
     @ $byvillage = $_POST['village']; 
     $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 village = '$byvillage'")or die(mysql_error("Error: querying the district")); 

     $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 District"; 
    } 
} 
+0

等都不是代碼審查的網站。你應該試着解決問題,展示你的嘗試並提出一個尖銳的問題。在常見問題解答中,您會發現如何提問的提示。不需要跳動。 – michi

+0

只需在每個查詢結束時使用GROUP BY您的任何重複字段。 –

+0

請問,「專業化」表格是什麼?它的內容有可能增加結果集的基數嗎? –

回答

0

一些簡單的事情值得嘗試:

  1. 回聲或在的var_dump $ _POST結果,以確保你得到你認爲你所得到的數據。這可能是導致問題的原因。你可能會得到多個

  2. 看來最有可能的是亂碼的問題的原因在這裏。您有三個輸出部分,因此您有三個可能的輸出,所有這些都可以在滿足適當的IF條件時觸發。嘗試在每個區塊中迴應'Here''There'和'Everywhere'或您選擇的單詞 - 您將很快看到可疑代碼的位置!

  3. 一個更好的選擇可能是您的where子句更改爲類似:

    WHERE village='[villagename from post]' OR district='[districtname from post'] OR governorate='[govname from post]' 
    

這樣,你只需要一個,如果和一個輸出中。它也可以更好地擴展。

  1. 嘗試運行與手動插入的測試數據的每個SQL(PHP myAdmin是此非常方便),看看是否問題是在SQL或php水平。

  2. @ - 試着避免它。最好用不會產生錯誤的方式編寫代碼。你有沒有把它放在一個空索引錯誤?如果是這樣,這可能是你的問題。檢查你的HTML並看到上面的答案1。