2017-02-19 58 views
0

我有一個表單,用戶輸入數據AXZAA QS1QS。這些數據發佈到我的PHP腳本。 PHP腳本連接到當前包含2條記錄的MYSQL數據庫。正在顯示不需要的數據

這個想法是,PHP腳本將接受輸入並將其與數據庫中的記錄進行比較。如果記錄存在,則它們將顯示在網頁的表格中,否則會顯示錯誤消息。

我的PHP腳本出現了很多問題,並且多次修改了我的腳本。然而,我遇到的最大問題是這樣的:

當表格第一次出現時,消息記錄不存在出現兩次,這是用戶輸入任何數據之前,並且正在看到形式第一次。見下圖。

enter image description here

輸入數據後(當PHP腳本部分正常工作),如果有匹配,即記錄存在,在表中的記錄一起我會收到一條錯誤消息告訴我的記錄是未找到。要查看我是否可以解決問題,我添加了代碼以告訴我哪些記錄找不到,無法找到的記錄是那些找到的記錄,以及我沒有查找的數據庫中的其他記錄。我知道我的PHP腳本中的SQL查詢告訴腳本從數據庫中獲取所有內容,但是我會認爲if語句可以解決問題。

抱歉寫了這麼長的問題,我希望它不會讓人困惑。

enter code here 
<?php 
    //Connect to the database connection file 
    require 'databaseconnection.php'; 

     $searchBar=(isset($_POST['searchBar']) ? $_POST['searchBar'] :null); 
     $userdata = trim($searchBar); 
     $cleaned_data = preg_split('/[\s]+/', $userdata); 


    $sql = "SELECT DISTINCT * FROM atable_2"; 

    $result = mysqli_query($database_connection, $sql); 

    echo "<table border> 
    <tr> 
     <th>Allocation</th> 
     <th>Codes</th> 
     <th>Names</th> 
     </tr>"; 



    while($putdatabaseanswer_intoarray = mysqli_fetch_array($result)) { 

    $allocation_id = $putdatabaseanswer_intoarray["allocation"]; 
    $codes_id = $putdatabaseanswer_intoarray["codes"]; 
    $names_id = $putdatabaseanswer_intoarray["names"]; 

     foreach($cleaned_data as $value) { 

     if($value==$codes_id) { 

      echo "<tr>"; 
      echo "<td>" . $allocation_id. "</td>"; 
      echo "<td>" . $codes_id . "</td>";  
      echo "<td>" . $names_id . "</td>"; 
      echo "</tr>"; 

     } 
     else 
     { 
     echo "<br />"; 
     echo "One or more of the records have not been found: $codes_id"; 
     echo"<br />"; 
     } 

     } 

    } 

    echo "</table>"; 
?> 

回答

0

那豈不是更好地後,如果語句像

`<?php 
    //Connect to the database connection file 
    require 'databaseconnection.php'; 

     if(isset($_POST['searchBar'])) 
{ 
    $searchbar = $_POST['searchBar']; 
     $userdata = trim($searchBar); 
     $cleaned_data = preg_split('/[\s]+/', $userdata); 


    $sql = "SELECT DISTINCT * FROM atable_2"; 

    $result = mysqli_query($database_connection, $sql); 

    echo "<table border> 
    <tr> 
     <th>Allocation</th> 
     <th>Codes</th> 
     <th>Names</th> 
     </tr>"; 



    while($putdatabaseanswer_intoarray = mysqli_fetch_array($result)) { 

    $allocation_id = $putdatabaseanswer_intoarray["allocation"]; 
    $codes_id = $putdatabaseanswer_intoarray["codes"]; 
    $names_id = $putdatabaseanswer_intoarray["names"]; 

     foreach($cleaned_data as $value) { 

     if($value==$codes_id) { 

      echo "<tr>"; 
      echo "<td>" . $allocation_id. "</td>"; 
      echo "<td>" . $codes_id . "</td>";  
      echo "<td>" . $names_id . "</td>"; 
      echo "</tr>"; 

     } 
     else 
     { 
     echo "<br />"; 
     echo "One or more of the records have not been found: $codes_id"; 
     echo"<br />"; 
     } 

     } 

    } 

    echo "</table>"; 
} 
    else{ 
    echo "<p>Please enter a search term</p>"; 
    } 
?> 

然後,您可以說:「如果」語句中執行MySQL查詢,而不是它執行的假設有分配$搜索欄一個值

+0

首先感謝您的幫助。 – wishicouldprogramme

+0

首先感謝您的幫助。我對代碼進行了一些更改,消息中的php代碼正好是我更改我的php腳本之前所做的。你提到在if語句中執行MYSQL查詢,你的意思是行$ sql =「SELECT DISTINCT * FROM atable_2」; \t \t \t $ result = mysqli_query($ database_connection,$ sql); – wishicouldprogramme

+0

難道你不覺得整個腳本只應該在發佈的值時執行嗎?因此,把if語句放在「require」後面,然後所有內容都應該放在大括號內。然後,您可以在「其他」中找到您想要的任何消息,並在頁面最初加載時顯示。 – Daniel