【问题标题】:How to generate incremental order number for multi-user environment如何为多用户环境生成增量订单号
【发布时间】:2021-05-03 03:51:30
【问题描述】:

希望每个人都做得很好。我正在尝试在多用户环境中生成增量订单/发票编号

此代码适用于单用户环境。但是如何在多用户环境中实现相同的目标。

例如,有两个用户(用户 A 和用户 B)

用户 A 生成了两张发票,那么订单/发票编号将是这样的 INV-0000001, INV-0000002

用户 B 生成了三张发票,那么订单/发票编号将是这样的 INV-0000001、INV-0000002、INV-0000003

用户表 user_id 是自增主键

User_id | user_name | user_email | user_pass
1       | xyz       | xyz        | xyz
2       | xyz       | xyz        | xyz

销售表

sale_id 是一个自动递增的主键,而 sale 表中的 user_id 充当外键 假设有一个 invoice_number 列!这就是我想要实现的目标


sale_id | user_id | invoice_number | sale_customer_name and so on
1       | 1       | INV-0000001    | xyz    
2       | 1       | INV-0000002    | xyz      
3       | 2       | INV-0000001    | xyz     
4       | 2       | INV-0000002    | xyz     
5       | 2       | INV-0000003    | xyz     

sale_id 是一个自动递增的 id

$select_invoice = "select * from sale where user_id='$user_id' order by sale_id desc limit 0,1";
$select_invoice_query = mysqli_query($connection, $select_invoice);
$invoice_array = mysqli_fetch_assoc($select_invoice_query);
$invoice_result = $invoice_array['sale_id'];
$invoice_result++;
$invoice_result = "INV-".str_pad($invoice_result, 7, "0", STR_PAD_LEFT);

【问题讨论】:

  • sale_id 是做什么用的?
  • sale_id 是一个自动递增的 id。 @Psycho
  • 这不是问题。当用户没有任何发票时,您将得不到任何结果。在这种情况下,您将手动从 1 开始。
  • 您能否通过编辑您的问题来提供您的表格的定义?
  • 阿汉!我现在明白你的意思了!所以这基本上会从每个用户的 0000001 开始发票编号,对吧? @El_Vanja

标签: php mysql


【解决方案1】:

首先:确保变量$user_id 具有值。然后将您的查询更改为:

$select_invoice = "select * from sale where user_id=$user_id order by sale_id desc";
$select_invoice_query = mysqli_query($connection, $select_invoice);
$invoice_array = mysqli_fetch_assoc($select_invoice_query);
$invoice_result = mysqli_num_rows($select_invoice_query);
$invoice_result++;
$invoice_result = "INV-".str_pad($invoice_result, 7, "0", STR_PAD_LEFT);

这将根据 user_id foreign_key 自动过滤发票结果,因此您只会获得当前/登录用户的发票。

澄清:

假设, 用户表:

| User_id | user_name | user_email | user_pass |
| ------- | --------- | ---------- | --------- |
| 1       | user A    | email A    | pass A    |
| 2       | user B    | email B    | pass B    |

注意: user_id 在这里是主键

销售额表:

| sale_id | user_id | sale_customer_name |
| ------- | ------- | ------------------ |
| 1       | 1       | sale 1             |
| 2       | 1       | sale 2             |
| 3       | 2       | sale 3             |
| 4       | 2       | sale 4             |
| 5       | 2       | sale 5             |

注意: user_id 在这里是外键主键sale_id

现在,当您尝试为特定用户获取销售额时,例如假设用户 A 已登录。请确保您从登录用户那里获得 user_id,可能来自会话,也可能取决于您的状态获取logged_in_user_info。因此,由于销售表中的 Where 条件 [select * from sales where user_id=$logged_in_user_id order by sale_id desc limit 0,1],查询将仅获得用户 A 的结果(2 个结果:sale1 和 sale2)。您正在销售表中运行查询。

现在,当用户 B 登录并从销售表中运行上述查询时,它将得到 3 个结果(sale3、sale4、sale5)。

现在,如果您有另一个用户 C,他到目前为止还没有发票。然后通过运行查询将返回 0 结果,因为没有用户 C 的发票。 where 条件过滤其他用户的结果并返回用户 C 的 0 结果。因此,用户 C 的发票必须是用户 C 的 INV-0000001 .

【讨论】:

  • 好的,你需要了!我的道歉。 $invoice_result = mysqli_num_rows($invoice_array);
  • 阿汉明白了!它只会计算特定用户的行数,然后据此递增,对吗?
  • 写完这段代码后错误消失了 $invoice_result = mysqli_num_rows($select_invoice_query);
  • 哦,现在限制1是问题,增加10或100
  • 不客气,我们已经在我的回答中发表评论了!你可以接受并投票!
【解决方案2】:

生成完之后再分配订单号就行了。

【讨论】:

  • 请分享更多细节,以便其他人可以从您的回答中学习。 “什么时候结束”是什么意思?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-15
  • 1970-01-01
  • 2019-11-03
  • 1970-01-01
  • 1970-01-01
  • 2020-02-14
  • 1970-01-01
相关资源
最近更新 更多