2013-04-09 96 views
-3

我試圖創建一個PHP頁面,您可以在多個給定的點上搜索數據庫 我寫了搜索部分,但我無法弄清楚如何顯示找到的項目 迄今爲止我的代碼是顯示搜索值

<html> 
<body> 
<link rel="stylesheet" href="css/reset.css" type="text/css" media="all"> 
<link rel="stylesheet" href="css/grid.css" type="text/css" media="all"> 
<link rel="stylesheet" href="css/home.css" type="text/css" media="all"> 
<?php 
$connectie = mysql_connect('localhost','root','usbw'); 
if($connectie == false){echo "error";} 

if(mysql_select_db('makelaars',$connectie) == false){ echo "error2";} 
?> 
<table> 
<tr> 
    <td width= 100> 
     Soort Object: 
    </td> 
    <td> 
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'> 
    <select name="soort"> 
     <?php 
      $query_soort = "SELECT DISTINCT `Soort_object` FROM `objecten`"; 
      $resultaat = mysql_query($query_soort, $connectie); 
      $inhoud_soort = $_POST['blog']; 
     while ($soort = mysql_fetch_assoc($resultaat)) { 
      echo "<option>" . htmlentities($soort['Soort_object']) . "</option>"; 
     } 
     ?> 
    </select> 
    </td> 
    </tr> 
    <tr> 
    <td> 
    <p>Status:</p> 
    </td> 
    <td> 
    <select name="soort"> 
     <?php 
      $query_status = "SELECT DISTINCT `status` FROM `objecten`"; 
      $resultaatstatus = mysql_query($query_status, $connectie); 
      $inhoud_status = $_POST['blog']; 
     while ($status = mysql_fetch_assoc($resultaatstatus)) { 
      echo "<option>" . htmlentities($status['status']) . "</option>"; 
     } 
     ?> 
    </select> 
    </td> 

</tr> 
<tr> 
    <td> 
    <p>Provincie:</p> 
    </td> 
    <td> 
    <select name="provincie"> 
     <?php 
      $query_provincie = "SELECT DISTINCT `Provincie` FROM `objecten`"; 
      $resultaatprovincie = mysql_query($query_provincie, $connectie); 
      $inhoud_provincie = $_POST['blog']; 
     while ($provincie = mysql_fetch_assoc($resultaatprovincie)) { 
      echo "<option>" . htmlentities($provincie['Provincie']) . "</option>"; 
     } 
     ?> 
    </select> 
    </td> 
    <td> 
     <input type="submit" value="verstuur" name="soortknop"> 
    </td> 

</tr> 
<tr> 

    <td> 

    </td> 
    </form> 

有人可以解釋我怎麼能顯示有說出來的搜索

+0

可能需要一些細節。你是在問一旦搜索發生了怎麼辦? – sbatson5 2013-04-09 20:04:00

回答

0

還沒有正確測試這個機會同值的數據,但它應該更多或更少的工作。請仔細看看代碼中的註釋,你有一個錯誤('select'標籤的名字),如果不是固定的,運行後會顯示錯誤!

在連接到數據庫之後進行此操作。你在做什麼基本上是檢查提交按鈕是否被點擊(然後設置與提交按鈕的名稱相對應的$ _POST屬性)。如果沒有提交,代碼將不會執行,結果將不會顯示。

我還建議你不要使用mysql_函數,而是看一下庫,比如PDO。其參數化查詢使您的腳本更安全並防止SQL注入。

<?php 

// Submit button was clicked 
if(isset($_POST['soortknop'])) { 

    // Sanitize data 
    $post_soort = mysql_real_escape_string($_POST['soort']); 
    $post_status = mysql_real_escape_string($_POST['status']); // changed this, you've two select fields 'soort'? 
    $post_provincie = mysql_real_escape_string($_POST['provincie']); 

    // Fetch results using all $post_ variables 
    $result_query = mysql_query(
     "SELECT * 
     FROM 'objecten' 
     WHERE Soort_object = '" . $post_soort . "' 
      AND status = '" . $post_status . "' 
      AND Provincie = '" . $post_provincie . "'"); 

    // Display results 
    while ($row = mysql_fetch_assoc($result_query)) { 
     echo htmlentities($row['Soort_object']) . " " . htmlentities($row['status']) . " " . htmlentities($row['post_provincie ']); 
    } 

} 
+0

感謝您的幫助,但它給了我一個錯誤,告訴我有一個錯誤的查詢 while($ row = mysql_fetch_assoc($ result_query)) – Damhuis 2013-04-09 21:20:40

+0

你是對的:)看來我錯過了一個結束撇號!檢查編輯後的代碼,現在它有'$ post_provincie。 ''「);' – 2013-04-09 21:23:34

+0

我仍然收到同樣的錯誤對不起,再次麻煩你,但我不知道還有什麼可能是錯的 – Damhuis 2013-04-09 21:31:30