douyan4900 2017-02-16 08:33
浏览 37

在codeigniter中使用php和mysql防止重复输入

Hi i am having trouble in inserting a tracking number in database. it seems that in some cases it generates a duplicate entry. I am generating the tracking number base on the last entry in my first_track table and increment it by 1. now my problem is that when ever the user clicks at the same time. it generates the same tracking number. how do i prevent it? btw here is my code in generating the tracking number. i am also returning the count to 0001 every 1st entry of each month.

<!----------Model-------->
            $this->db->order_by("first_trackid", "desc");
        $query = $this->db->get('first_track');
        if($query->num_rows() > 0)
        {
            $result = $query->result();
            if(date('m') != substr($result[0]->dtsno,2,2)){
                $dtsno = date('ym').'0001';                 
                }
            else{
                $dtsno = $result[0]->dtsno+1;
            }
            return  $dtsno;

        }
        else
        {
            return  $dtsno = date('ym').'0001';                 
        }
<!--- END model------->
<!---controller----------->
//call the model for generating dtsno
$firsttrack->dtsno = $this->user_information_model->dtsno();
//insert to table first_entry
$this->user_information_model->first_track($firsttrack);
  • 写回答

1条回答 默认 最新

  • douji5523 2017-02-16 08:42
    关注

    First of all, in order to ensure that you do not get duplicated values in the database, make sure you index(Set it as unique) the column ("first_trackid") which is holding the tracking number in the table first_track.

    Second, you make use of a temporary track sequence number based on timestamp, when the user initiates the process.

    The actual generation of tracking number should take place when the user goes to complete the whole process or in other words, saves the record. At that time, generate that number and display to the user accordingly. In that way, you can ensure that the values will never be duplicated in your schema.

    Regards

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?