我正在实施一个系统,在产品进入和缺货时跟踪所有实例。我在表格中有一个列,用于跟踪给定记录是打开还是关闭(打开意味着它缺货并且没有重新进货和关闭意味着它在某个时刻重新投入库存)。到目前为止,在确定它们有效时,我没有遇到过SQL查询的那么多问题。但是,我一直在运行用于此表填充的脚本,并且没有任何内容插入到表中。
我用于此特定表更新的函数如下所示。
//Records item as being back in stock
function itemInStock($item){
$db_controller = new DBHandler();
$selectQuery = "SELECT * FROM out_of_stock_record
WHERE ListID = '$item->ID'
AND Status = 'OPEN'";
$rs = $db_controller->select($selectQuery);
/*If this record exists, then the only thing we need to do is update the BackInStockTS and Status columns. It came back in stock on this date at this time, therefore it's closed.*/
if($rs){
$currentDate = date("Y-m-d H:i:s");
$updateQuery = "UPDATE out_of_stock_record
SET BackInStockTS = '$currentDate', Status = 'CLOSED'
WHERE ID = '$item->ListID' AND Status = 'OPEN'";
$db_controller->update($updateQuery);
}
}
//Records item as being out of stock
function itemOOStock($item){
$db_controller = new DBHandler();
$selectQuery = "SELECT ID FROM out_of_stock_record
WHERE ID = '$item->ID'
AND Status = 'OPEN'";
$rs = $db_controller->select($selectQuery);
/*We only need to make sure the record DOESN'T exist, because if that's true, then all we need to do is insert this. If it already exists, there's nothing to do.
The only other check occurs later on that sees if the item is back in stock, then updates the existing open record in order to close it and record the date it
came back in stock.*/
if(!$rs){
$currentDate = date("Y-m-d H:i:s");
$start = "INSERT INTO out_of_stock_record (ID, OutOfStockTS, BackInStockTS, Status) VALUES (";
$end = sprintf("'%s', '%s', NULL, '%s')", $item->ID, $currentDate, "OPEN");
$start .= $end;
$db_controller->insert($start);
}
}
我不知道问题是什么。我使用完全相同的数据库处理程序插入此脚本的另一部分中的不同命名的表,我没有问题,我不确定它与表的结构有关。它由ID和状态的varchar列,OOS和IS日期的datetime列组成,还包括另一列用于记录此表的上次更新,但只是在插入时自动更改。
除了我无意中创建并稍后更正的一些脚本之前,我没有遇到任何问题。每次访问此页面的URL时,该表都会更新。有关可能导致此问题的任何建议?如果有帮助,此脚本使用的数据库以及托管在其上的站点位于同一服务器上。
答案 0 :(得分:1)
我对if语句使用了错误的检查。他们应该看起来像这样。
if(mysql_num_rows($rs) == 0){
//Insert record
}
if(mysql_num_rows($rs) != 0){
//Update record
}