-3

我需要使用 php mysql 准备发票,我已经准备好了,我的问题是每年的发票编号都重置为第一个。

例如:2013 - 1、2013 - 2、2013 -3。2014 年 1 月、2014 年 2 月、2014 年 3 月。

我的桌子

CREATE TABLE IF NOT EXISTS `gen` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`invno` int(11),
`prefix` varchar(500) DEFAULT NULL,
`year` year(4) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`invno`,`year`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

代码是

$year = date("Y", time());

$sql = "SELECT year FROM gen where year = '$year' ";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
$inv = '0';

if ($count == 0) {
    $inv = '0';
} else {
    $list = mysql_fetch_assoc($result);
    $inv = $list['invno'];
}

if ($inv == '0') {
    $inv = '1';
} else {
    $inv++;
}

echo $inv;

当我执行时它显示“1”,表中有更多发票。我在单独的字段中设置了年份。我认为如果在该年发票编号中找不到任何行,则 sql 查询检查表中的年份应设置为 1,否则会增加先前的编号。

4

3 回答 3

0

你忘了初始化$inv

尝试

$count = mysql_num_rows($result);
$inv = '0';
if($count == '0')
{
$inv = '0';
.....

您也没有invno在查询中选择

$sql="SELECT year FROM gen where year = '$year' ";
$inv = $list['invno'];


$sql="SELECT max(invno) FROM gen where year = '$year' ";// may be this is what you want
于 2013-08-14T05:47:39.893 回答
0

嘿首先数字是一个字符串。删除 '' 并仅输入数字。如果($count == 0)

于 2013-08-14T05:49:20.517 回答
0

这会对你有所帮助。

//gets the maximum number of till the date
$result=$db->query("SELECT max(date),max(billno) FROM invoice;");
  $billno = $result->fetch_row()[1];
  $billno+=1;
  //if the the new year begans the it will reset bill number to 1
  if (date("m")==1 && date("d")==1) {
    $billno=1;  
    }
    echo $billno;```
于 2021-08-31T06:40:31.900 回答