dongzi4030 2013-07-10 07:36
浏览 53

创建唯一帐单标识符

I would like to create a unique billing identifier/number. I want it to be a combination of a constant string, year and an additional number. For example "ABC-2013-100001". How can I do that so I will be definitely sure that the billing identifier is unique? I was thinking about maintaing a billing_id like a primary key that would be unique and autoincrement. Then I would just select max(billing_id) to get the last one add 1 to it and the prefix "ABC-2013" and then insert it into the table. The problem is that this is a 3 step way. Select, do something, insert. I am afraid that it might happen something like, when 2 users will do this at the same time, every one selects the same number, so technically they insert the same identifier. Am I wrong and overreacting or is this kind of generating unique ids common? Should I take a different approach?

EDIT

Ok so I did something like this:

$data = array(
    'billing_identifier' => 'law-'.$year."-".$this->db->insert_id()
);
// inserting into billing_identifiers
    $this->db->insert('billing_identifiers', $data);

    $bill_id = $this->db->insert_id();

but the $this->db->insert_id() is 0 at the beginning which makes sense and after calling the insert function it gets the last id. Isnt there a way I can do that in one shot?

  • 写回答

3条回答 默认 最新

  • duanmei1922 2013-07-10 07:44
    关注

    Just use this, it will give you the primary id.

    <?php
    $con=mysqli_connect("127.0.0.1","user","pass","db");
    //query here
    $id = mysqli_insert_id($con);
    
    $bill_id = 'ABC-2013-'. $id;
    
    ?>
    

    changed because of "doesn't fit the question"...

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。