2017-09-15 106 views
-2

我需要幫助,請,我儘量讓層次組織PHP應用程序,然後我設置了以下結構的數據庫:子查詢內部While循環

table

基本上,我的列表所有員工的姓名。 每個員工的行在連續列(c5到c10)上包含他所依賴的服務的名稱(降序層次結構)。 當我從下拉列表中點擊特定的員工姓名時,我將其「id」發送給應該返回到兩列表中的查詢:

-1。在第二列中,所有員工選擇的層次遞減的服務都依賴於這些服務。 -2。在第一欄中,第一欄中顯示了各個部門或服務的經理。

要找到服務的名稱,例如johana,請求對我來說很簡單。

要找到經理的姓名,對約翰娜例如,我要運行從她所依賴的服務4個不同的請求:

-1。用於市場營銷:查詢返回字段「c5」包含「市場營銷」字段「name」和字段「c6」爲空的人員姓名。 -2。對於歐洲:查詢返回字段「c6」中包含「歐洲」的字段「name」中的人員姓名,並且字段c7爲空。 -3。用於宣傳:查詢返回字段「c7」中包含「宣傳」的字段「name」中的人員姓名,並且字段c8爲空。 -4。對於電視:宣傳:查詢返回字段「c8」包含「電視」,字段c9爲空的字段「名稱」中的人名。 PS:對於其他需要,我插入一列表示最後一個單元格的列號不爲空,另一列表示此單元格的值。

這裏是我的代碼:

<?php 
require_once '../config/dbconnect.php'; 
if(isset($_POST["prs_id"]) && !empty($_POST["prs_id"])){ 
    $result = $conn->query("SELECT * FROM mytable WHERE id = ".$_POST['prs_id'].""); 
    $rowCount = $result->num_rows; 
    if($rowCount > 0){ 
     while($row = $result->fetch_assoc()){ 
      // utile variables ------------------- 
      $numcol = $row['lastcol']; 
      $currcol = 'c' . $row['lastcol']; 
      $currval = $row['lastval']; 
      $prevcol = 'c' . ($row['lastcol'] - 1); 
      $prevval = $row[$prevcol]; 
      $nextcol = 'c' . ($row['lastcol'] + 1); 
      $nextval = $row[$nextcol]; 
      // ----------------------------------- 
      echo '<tr><td style="width:296px;">'; 
      echo 'query to find manager name level 1'; // returned value of query("SELECT name FROM mytable WHERE c5 = $row['c5'] AND c6 = "" "); 
      echo '</td><td>'; 
      echo $row['c5']; 
      echo '</tr><tr><td>'; 
      echo 'query to find manager name level 2'; // returned value of query("SELECT name FROM mytable WHERE c6 = $row['c6'] AND c7 = "" "); 
      echo '</td><td>'; 
      echo $row['c6']; 
      echo '</tr><tr><td>'; 
      echo 'query to find manager name level 3'; // returned value of query("SELECT name FROM mytable WHERE c7 = $row['c7'] AND c8 = "" "); 
      echo '</td><td>'; 
      echo $row['c7']; 
      echo '</tr><tr><td>'; 
      echo 'query to find manager name level 4'; // returned value of query("SELECT name FROM mytable WHERE c8 = $row['c8'] AND c9 = "" "); 
      echo '</td><td>'; 
      echo $row['c8']; 
      echo '</tr><tr><td>'; 
      echo 'query to find manager name level 5'; // returned value of query("SELECT name FROM mytable WHERE c9 = $row['c9'] AND c9 = "" "); 
      echo '</td><td>'; 
      echo $row['c9']; 
      echo '</tr><tr><td>'; 
      echo 'employee name clicked in case his lastvalue is in c10'; 
      echo '</td><td>'; 
      echo $row['c10']; 
      echo '</td></tr>'; 
     } 
    } 
} 

>

+0

你有問題嗎? – BizzyBob

+0

聽說過SQL注入? – Akintunde007

+0

你有什麼問題/問題? – MikeMayer67

回答

0

我發現我的問題的解決方案,所以我將它張貼他人?

<?php 
require_once '../config/dbconnect.php'; 
if(!empty($_POST["prs_id"])) { 
    $result = $conn->query("SELECT DISTINCT * FROM mytable WHERE id = '".mysqli_escape_string($conn, $_POST['prs_id'])."' "); 
    $rowCount = $result->num_rows; 
    if($rowCount > 0){ 
     while($row = $result->fetch_assoc()){ 
      $numcol = $row['lastcol']; 
      $currcol = 'c' . $row['lastcol']; 
      echo '<div>'; 
      if($currcol == 'c5'){ 
       echo '<tr><td>'; 
       $rsl5 = $conn->query("SELECT name FROM mytable WHERE c5 = '".mysqli_escape_string($conn, $row['c5'])."' AND c6 = '' "); 
       $cnt5 = $rsl5->num_rows; 
       while($rw5 = $rsl5->fetch_assoc()){ 
        if($cnt5 == 1){ 
         echo $rw5['name']; 
        } else { 
         echo $rw5['name']; 
         echo '<br />'; 
        } 
       } 
       echo '</td><td>'; 
       echo $row['c5']; 
       echo '</td></tr></table></div>'; 
      } else if($currcol == 'c10') { 
       echo '<tr><td>'; 
       $rsl5 = $conn->query("SELECT name FROM mytable WHERE c5 = '".mysqli_escape_string($conn, $row['c5'])."' AND c6 = '' "); 
       $cnt5 = $rsl5->num_rows; 
       while($rw5 = $rsl5->fetch_assoc()){ 
        if($cnt5 == 1){ 
         echo $rw5['name']; 
        } else { 
         echo $rw5['name']; 
         echo '<br />'; 
        } 
       } 
       echo '</td><td>'; 
       echo $row['c5']; 
       echo '</td></tr>'; 
       echo '<tr><td>'; 
       $rsl6 = $conn->query("SELECT name FROM mytable WHERE c5 = '".mysqli_escape_string($conn, $row['c5'])."' AND c6 = '".mysqli_escape_string($conn, $row['c6'])."' AND c7 = '' "); 
       $cnt6 = $rsl6->num_rows; 
       while($rw6 = $rsl6->fetch_assoc()){ 
        if($cnt6 == 1){ 
         echo $rw6['name']; 
        } else { 
         echo $rw6['name']; 
         echo '<br />'; 
        } 
       } 
       echo '</td><td>'; 
       echo $row['c6']; 
       echo '</td></tr>'; 
       echo '<tr><td>'; 
       $rsl7 = $conn->query("SELECT name FROM mytable WHERE c6 = '".mysqli_escape_string($conn, $row['c6'])."' AND c7 = '".mysqli_escape_string($conn, $row['c7'])."' AND c8 = '' "); 
       $cnt7 = $rsl7->num_rows; 
       while($rw7 = $rsl7->fetch_assoc()){ 
        if($cnt7 == 1){ 
         echo $rw7['name']; 
        } else { 
         echo $rw7['name']; 
         echo '<br />'; 
        } 
       } 
       echo '</td><td>'; 
       echo $row['c7']; 
       echo '</td></tr>'; 
       echo '<tr><td>'; 
       $rsl8 = $conn->query("SELECT name FROM mytable WHERE c7 = '".mysqli_escape_string($conn, $row['c7'])."' AND c8 = '".mysqli_escape_string($conn, $row['c8'])."' AND c9 = '' "); 
       $cnt8 = $rsl8->num_rows; 
       while($rw8 = $rsl8->fetch_assoc()){ 
        if($cnt8 == 1){ 
         echo $rw8['name']; 
        } else { 
         echo $rw8['name']; 
         echo '<br />'; 
        } 
       } 
       echo '</td><td>'; 
       echo $row['c8']; 
       echo '</td></tr>'; 
       echo '<tr><td>'; 
       $rsl9 = $conn->query("SELECT name FROM mytable WHERE c8 = '".mysqli_escape_string($conn, $row['c8'])."' AND c9 = '".mysqli_escape_string($conn, $row['c9'])."' AND c10 = '' "); 
       $cnt9 = $rsl9->num_rows; 
       while($rw9 = $rsl9->fetch_assoc()){ 
        if($cnt9 == 1){ 
         echo $rw9['name']; 
        } else { 
         echo $rw9['name']; 
         echo '<br />'; 
        } 
       } 
       echo '</td><td>'; 
       echo $row['c9']; 
       echo '</td></tr>'; 
       echo '<tr><td>'; 
       $rsl10 = $conn->query("SELECT name FROM mytable WHERE c9 = '".mysqli_escape_string($conn, $row['c9'])."' AND c10 = '".mysqli_escape_string($conn, $row['c10'])."' "); 
       $cnt10 = $rsl10->num_rows; 
       while($rw10 = $rsl10->fetch_assoc()){ 
        if($cnt10 == 1){ 
         echo $rw10['name']; 
        } else { 
         echo $rw10['name']; 
         echo '<br />'; 
        } 
       } 
       echo '</td><td>'; 
       echo $row['c10']; 
       echo '</td></tr></table></div>'; 
      } else { 
       echo '<tr><td>'; 
       $rsl5 = $conn->query("SELECT name FROM mytable WHERE c5 = '".mysqli_escape_string($conn, $row['c5'])."' AND c6 = '' "); 
       $cnt5 = $rsl5->num_rows; 
       while($rw5 = $rsl5->fetch_assoc()){ 
        if($cnt5 == 1){ 
         echo $rw5['name']; 
        } else { 
         echo $rw5['name']; 
         echo '<br />'; 
        } 
       } 
       echo '</td><td>'; 
       echo $row['c5']; 
       echo '</td></tr>'; 
       for ($i=6; $i<=$numcol; $i++) { 
        $rsl = 'rsl' . $i; 
        $rw = 'rw' . $i; 
        $cnt = 'cnt' . $i; 
        $currcln = 'c' . $i; 
        $prevcln = 'c' . ($i - 1); 
        $nextcln = 'c' . ($i + 1); 
        echo '<tr><td>'; 
        $rsl = $conn->query("SELECT name FROM mytable WHERE ".$prevcln." = '".mysqli_escape_string($conn, $row[$prevcln])."' AND ".$currcln." = '".mysqli_escape_string($conn, $row[$currcln])."' AND ".$nextcln." = '' "); 
        $cnt = $rsl->num_rows; 
        while($rw = $rsl->fetch_assoc()){ 
         if($cnt == 1){ 
          echo $rw['name']; 
         } else { 
          echo $rw['name']; 
          echo '<br />'; 
         } 
        } 
        echo '</td><td>'; 
        echo $row[$currcln]; 
        echo '</td></tr>'; 
       } 
       echo '</table></div>'; 
      } 

     } 
    } 
} 
?>