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

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP