到目前为止,我有以下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内容中?例如,在搜索按钮下方。
答案 0 :(得分:1)
解决方案是不回显它将在顶部显示的内容,因为PHP首先处理代码然后加载html因此它将回显页面顶部的数据。
解决方案是将结果分配到while循环中的数组中,然后使用该数组在html中显示它。
答案 1 :(得分:0)
也许,关闭标签'p'会有帮助吗?尝试添加
echo "</p>";
最后。
答案 2 :(得分: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>";
如果你允许稍微加时答案,你的代码就会告诉我你正在学习PHP。
如果是这样,请记住,在线PHP教程经常在几年前编写,并且处于过时状态,完整的示例代码显示了您可以做的最糟糕的事情。
假设我的评估是正确的,请考虑学习PHP以及设计良好的框架。在这方面,Silex相对容易接受:
您可能希望了解的其他框架:
还有很多很多。