Php 格式化数据库查询的结果
到目前为止,我有下面的php,它允许用户在数据库的特定位置进行搜索,并显示所有结果Php 格式化数据库查询的结果,php,html,mysql,Php,Html,Mysql,到目前为止,我有下面的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
<?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>";
}
}
也许,结束标记“p”会有帮助?尝试添加
echo "</p>";
echo“”;
最后。解决方案是不要回显它将在顶部显示的内容,因为PHP首先处理代码,然后加载html,因此它将回显页面顶部的数据
解决方案是将结果分配到while循环中的一个数组中,然后使用该数组以html格式显示它。我认为其中缺少一些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>";
echo”“;
echo$results['First_Name'];
回声“;
echo$results[‘姓氏’];
回声“,”;
echo$results['Company_Name'];
回声“
您可能需要研究的其他框架:
- (变老了)
还有很多很多。由于$field
,您有可能进行SQL注入。