2013-11-24 64 views
0

這是顯示建築物的所有樓層,我想顯示選定的建築物樓層我怎麼能這樣做我使用這個鏈接floors.php?id = Building1但它不工作請幫助我如何在mysql中獲取id

如果我寫在那裏它號樓工作正常where buildingname='building1'

,如果我用這一個where buildingname='$id'它不工作

我怎麼可以使用這樣

floors.php?ID =號樓

,如果我進入這個鏈接,然後它會顯示所有選定的建築結果

<?php 
    $max_results = 8; 

    $from = (($page * $max_results) - $max_results); 


    if(empty($_POST)) { 
        $query = "SELECT * FROM floors where buildingname='$id' ORDER BY floorno ASC LIMIT $from, $max_results "; 
    } 
    $result = mysql_query("SET NAMES utf8"); //the main trick 
    $result = mysql_query($query) or die(mysql_error()); 
    $rows = mysql_num_rows($result); 

    $count=0; 
    while($row = mysql_fetch_array($result)) 
     { 
      if($count%4==0) 
      { 
      echo "<tr/>"; 
      echo "<tr>"; 
      } 

      echo "<td><div align='center'><img src='images/floor.gif' width='60' height='90'></a><p>" . $row['floorno'] . "</p><div></td>"; 

      $count++; 


    } 



    echo "</tr>"; 
    echo "</table>"; 
    echo '</div>'; 
    ?> 
+2

'$ ID = $ _GET [ '身份證'];'靠近你的腳本的頂部。 –

+0

謝謝@BurhanKhalid – user3000993

+0

@ user3000993 like'$ _GET ['id']',我認爲'$ page'需要改變'$ _GET ['page']' –

回答

0
if(empty($_POST)) { 
    $query = "SELECT * FROM floors where buildingname='$id' ORDER BY floorno ASC LIMIT $from, $max_results "; 
} 

右邊是:

if (isset($_GET['id'])) { 
    $id = filter_input(INPUT_GET, 'id'); 
    $query = "SELECT * FROM floors where buildingname='$id' ORDER BY floorno ASC LIMIT $from, $max_results "; 
} 
-2

,可以幫助你。

<?php 
    $id =isset($_GET['id'])?$_GET['id']:null; // here you put value in $id only if it has some value. 
    $max_results = 8; 

    $from = (($page * $max_results) - $max_results); 


    if($id != null) { 
     $query = sprintf("SELECT * FROM floors WHERE buildingname='%s' ORDER BY floorno ASC LIMIT $from, $max_results", mysql_real_escape_string($id)); // safe from sql injection 
     $result = mysql_query("SET NAMES utf8"); //the main trick 
     $result = mysql_query($query) or die(mysql_error()); 
     $rows = mysql_num_rows($result); 


     $count=0; 
     while($row = mysql_fetch_array($result)) 
     { 
     if($count%4==0) 
     { 
     echo "<tr/>"; 
     echo "<tr>"; 
     } 

     echo "<td><div align='center'><img src='images/floor.gif' width='60' height='90'></a><p>" . $row['floorno'] . "</p><div></td>"; 

     $count++; 
     } 
    } 


    echo "</tr>"; 
    echo "</table>"; 
    echo '</div>'; 
?> 
+0

它的不好的建議,它很容易SQL注入,並且使用不推薦的SQL函數。 – nrathaus

+0

是的,相當大的他沒有使用準備好的聲明,但是是最糟糕的代碼生產。 –

+0

現在你擠了一個隨機?進入查詢它根本不會工作。 – Mchl