我在这里的第一篇文章,即使我是这个非常有用的网站的常客
我想知道是否有人可以帮助我解决以下问题:
我正在用 MySQL 为我的公司编写一个简单的库存检查解决方案,我仍然是新手,但正在学习。
我们有以下搜索表单,当输入产品的条形码时显示来自数据库的结果,我要做的是在结果底部有一个按钮,上面写着“检查确定”,它将信息重新提交给数据库,更新时间戳,并有一个隐藏字段将“状态”更改为已检查。
数据库名称为“stock” 表名称为“main_stock” 字段为:id curr_timestamp mastercategory category product_desc 条码序列状态
每次搜索只给出一个结果,因为条形码是唯一的
我坚持的一点是如何将结果转换为表格以便能够将它们重新提交回数据库
这仅供受保护网络内部使用,非常感谢任何帮助
谢谢
乔恩
<?php
$dbHost = 'localhost'; // localhost will be used in most cases
// set these to your mysql database username and password.
$dbUser = 'xxxxxxxx';
$dbPass = 'xxxxxxx';
$dbDatabase = 'stock'; // the database you put the table into.
$con = mysql_connect($dbHost, $dbUser, $dbPass) or trigger_error("Failed to connect to MySQL Server. Error: " . mysql_error());
mysql_select_db($dbDatabase) or trigger_error("Failed to connect to database {$dbDatabase}. Error: " . mysql_error());
// Set up our error check and result check array
$error = array();
$results = array();
// First check if a form was submitted.
// Since this is a search we will use $_GET
if (isset($_GET['search'])) {
$searchTerms = trim($_GET['search']);
$searchTerms = strip_tags($searchTerms); // remove any html/javascript.
if (strlen($searchTerms) < 2) {
$error[] = "Search terms must be longer than 2 characters.";
}else {
$searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
}
// If there are no errors, lets get the search going.
if (count($error) < 1) {
$searchSQL = "SELECT mastercategory, category, product_desc, barcode, serial FROM main_stock WHERE ";
// grab the search types.
$types = array();
$types[] = isset($_GET['barcode'])?"`barcode` LIKE '%{$searchTermDB}%'":'';
$types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)
if (count($types) < 1)
$types[] = "`barcode` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked
$andOr = isset($_GET['matchall'])?'AND':'OR';
$searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `barcode`"; // order by title.
$searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");
if (mysql_num_rows($searchResult) < 1) {
$error[] = "The scanned barcode {$searchTerms} is not in the database.";
}else {
$results = array(); // the result array
$i = 1;
while ($row = mysql_fetch_assoc($searchResult)) {
$results[] = "{$i}:Product Name : <br /> {$row['product_desc']} <br />Master Category : <br /> {$row['mastercategory']}<br />Sub Category : <br /> {$row['category']}<br />Barcode : {$row['barcode']} <br />Serial No. : {$row['serial']}";
$i++;
}
}
}
}
function removeEmpty($var) {
return (!empty($var));
}?>
<html>
<title>Search Form</title>
<BODY onLoad="document.forms.searchForm.search.focus()">
<form action="index.php">
<center>
<span class="formcentjc">
<input type=submit value="Home" />
</span>
</center>
</form>
<?php echo (count($error) > 0)?"The following had errors:<br /><span id=\"error\">" . implode("<br />", $error) . "</span><br /><br />":""; ?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="GET" name="searchForm" class="cent">
<onLoad="document.searchForm.search()">
<table width="196" border="1">
<tr>
<th bgcolor="#D6D6D6" style="text-align: center" scope="col">Search For:</th>
</tr>
<tr style="text-align: center">
<td class="cent1">
<input name="search" type="text" onFocus="this.value='';" value="<?php echo isset ($searchTerms)?htmlspecialchars($searchTerms):''; ?>" size="28" maxlength="15" />
<span style="text-align: center"></span></td>
</tr>
</table>
</form>
<p>
<tr>
<td bgcolor="#D6D6D6"><form action="addproduct.php">
<center>
<span class="formcentjc">
<input type=submit value="Add New Product" />
</span>
</center>
</form></td>
</tr>
<tr>
</p>
<?php
echo (count($results) > 0)?"SUCCESS: {$searchTerms} :<br /><br />" . implode("", $results):"";
?>
</body>
</html>