2017-02-19 62 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