0

到目前为止,我有以下 php,它允许用户在数据库的特定列中搜索并显示所有结果。

<?php
require("header.php");
if(isset($_REQUEST['searching'])){ //check if form has been submitted
echo"<h2>Results</H2><p>";
connect('final');//connect to DB
//set the values from search form
$field = $_POST['field'];
$query = $_POST['query']; 

    $query = htmlspecialchars($query); // stop HTML characters
    $query = mysql_real_escape_string($query); //stop SQL injection

     $data = mysql_query("SELECT *
     FROM customer 
     INNER JOIN address ON customer.ID = address.customer_ID
     LEFT OUTER JOIN sites ON address.ID = sites.address_ID 
     WHERE customer.ID IN (SELECT customer.ID 
     FROM customer
     INNER JOIN address ON customer.ID = address.customer_ID 
     LEFT OUTER JOIN sites ON address.ID = sites.address_ID
     WHERE upper(customer.$field) LIKE'%$query%')") ;//query the DB with search field in colleumn selected//

     //$data = mysql_query("SELECT * FROM customer INNER JOIN address ON customer.ID = address.Customer_ID LEFT OUTER JOIN sites ON address.ID = sites.address_ID WHERE upper(customer.$field) LIKE'%$query%'") ;

    if($data === FALSE) {
    $error = 'Query error:'.mysql_error();
    echo $error;
    }
    else
    {
    while($results = mysql_fetch_array($data)){// puts data from database into array, loops until no more
            echo "<br>"; 
            echo $results['First_Name']; 
            echo " "; 
            echo $results['Surname']; 
            echo " "; 
            echo $results['Company_Name']; 
            echo " "; 
            echo $results['Telephone']; 
            echo " "; 
            echo $results['Alt_Telephone']; 
            echo " "; 
            echo $results['line_1']; 
            echo " "; 
            echo $results['line2']; 
            echo " "; 
            echo $results['town']; 
            echo " "; 
            echo $results['postcode'];
            echo " "; 
            echo $results['site_name'];


            //posts results from db query
        }
    }


        $anymatches=mysql_num_rows($data); //checks if the querys returned any results
            if ($anymatches == 0) 
                    { 
                        echo "Sorry, but we can not find an entry to match your query<br><br>"; 
                    } 

    } 

但是,当我使用 echo 输出时,它只会在页面顶部显示所有结果,并将我的所有 html 内容推到它下方。

如何格式化它以使其出现在我的 html 内容中?例如,在搜索按钮下方。

4

3 回答 3

1

解决方案是不要回显它将在顶部显示的内容,因为 PHP 先处理代码然后加载 html,因此它将在页面顶部回显数据。

解决方案是在 while 循环中将结果分配到一个数组中,然后使用该数组在 html 中显示它。

于 2013-04-25T07:43:53.173 回答
0

也许,结束标签“p”会有所帮助?尝试添加

echo "</p>";

到底。

于 2013-04-25T07:43:42.157 回答
0

我认为你在那里缺少一些 html 标签,例如:

        echo "<div>"; 
        echo $results['First_Name']; 
        echo " "; 
        echo $results['Surname']; 
        echo ", "; 
        echo $results['Company_Name']; 
        echo "</div><address>"; 
        echo $results['Telephone']; 
        echo "<br>"; 
        echo $results['Alt_Telephone']; 
        echo "<br>"; 
        echo $results['line_1']; 
        echo "<br>"; 
        echo $results['line2']; 
        echo "<br>"; 
        echo $results['town']; 
        echo " "; 
        echo $results['postcode'];
        echo "</address><div>"; 
        echo $results['site_name'];
        echo "</div>"; 

如果您允许稍微 OT 的答案,您的代码表明您正在学习 PHP。

如果是这样,请记住,在线 PHP 教程经常是多年前编写的,并且处于过时状态,并附有示例代码,显示了您可以做的最糟糕的事情。

假设我的评估是正确的,请考虑在设计良好的框架旁边学习 PHP。Silex 在这方面比较容易上手:

您可能想要研究的其他框架:

还有很多很多。

于 2013-04-25T09:18:56.693 回答