我有一个包含库存商品的数据库和一个网页,允许您在数据库中搜索商品类型或特定商品编号。结果将返回并显示在搜索表单下的表格中。
目前,我通过将 mysql 查询的结果保存到数组中,然后将数组的结果打印到表中来做到这一点。
如何使结果可选,然后有一个按钮,例如“添加”,它允许我仅基于所选项目从数据库中检索信息。
编辑 -
<?php
require("header.php");
if(isset($_REQUEST['searching'])){ //check if form has been submitted
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 stock
WHERE stock.Part_Number IN (SELECT stock.Part_Number
FROM stock
WHERE upper(stock.$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
{
$test = array();
$colNames = array();
while($results = mysql_fetch_assoc($data)){// puts data from database into array, loops until no more
$test[] = $results;
}
$colNames = array_keys(reset($test));
$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>";
}
}
然后在我的html表单下面我创建一个表并有
<?php
//print the header
foreach($colNames as $colName)
{
echo "<th>$colName</th>";
}
?>
和
<?php
//print the rows
foreach($test as $results)
{
echo "<tr>";
foreach($colNames as $colName)
{
echo "<td>".$results[$colName]."</td>";
}
echo "</tr>";
}
?>
我不确定从哪里开始,但我会尝试使用复选框方法。