成千上万的插入物和我有重复的值,为什么?

时间:2018-09-03 14:22:59

标签: php android mysql laravel post

我有一个停车场系统应用程序,我的客户经常使用它来向我的数据库发送汽车中的数据条目。

这是要发送到我的服务器的代码:

 public void sendEntradaRotativaDataToServer(String url){

    pDialog = new ProgressDialog(getActivity());
    pDialog.setMessage("Processando entrada..");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(false);
    pDialog.show();

    JsonObject json = new JsonObject();
    json.addProperty("placa",  entradaObj.getPlaca());
    json.addProperty("dataemissao",  entradaObj.getDataemissao());
    json.addProperty("tipo",   entradaObj.getTipo());
    json.addProperty("valor",  entradaObj.getValor());
    json.addProperty("area",   entradaObj.getArea().getId());
    json.addProperty("observacao",  entradaObj.getObservacao());
    json.addProperty("user_id",  entradaObj.getUser_id());

    Ion.with(getActivity())
            .load(url)
            .setTimeout(5000)
            .setJsonObjectBody(json)
            .asJsonObject()
            .setCallback(new FutureCallback<JsonObject>() {
                @Override
                public void onCompleted(Exception e, JsonObject result) {

                    if (e != null) {

                        newMsgWarn("Atenção","Não foi possível fazer uma conexão por favor tente novamente");

                        if(pDialog != null && pDialog.isShowing()){
                            pDialog.dismiss();
                        }

                    } else {

                        success   = result.get(TAG_SUCCESS).getAsInt();
                        message   = result.get(TAG_MESSAGE).getAsString();

                        entradaObj.set_id(result.get("entradaid").getAsString());

                        (new SyncEntrada()).execute(new String[0]);

                        if(pDialog != null && pDialog.isShowing()){
                            pDialog.dismiss();
                        }

                    }

                }


            });

}

这是离子库的一个简单的发布请求,这是服务器上的代码(我在项目上使用laravel):

public function insertDataRotativa(Request $request) {


    if(isset($request)) {

        $dataentradaini = Carbon::parse($request->input('dataemissao'));           
        $evento   = $this->mdUtil->getEventByDataEmissao($dataentradaini);

        if (!empty($evento)) {

          foreach ($evento as $value) {  

              $this->mdEntrada                 = new Entrada; 
              $this->mdEntrada->placa          = $request->input('placa');
              $this->mdEntrada->created_at     = $dataentradaini;
              $this->mdEntrada->updated_at     = $dataentradaini;
              $this->mdEntrada->tipo           = $request->input('tipo');
              $this->mdEntrada->valor          = $request->input('valor');
              $this->mdEntrada->area           = $request->input('area');
              $this->mdEntrada->observacao     = $request->input('observacao');
              $this->mdEntrada->user_id        = $request->input('user_id');
              $this->mdEntrada->evento_id      = $value->id;    
              $this->mdEntrada->flag           = "U";                  
              $this->mdEntrada->save();

          }             

       }else {


              $this->mdEntrada                 = new Entrada; 
              $this->mdEntrada->placa          = $request->input('placa');
              $this->mdEntrada->created_at     = $dataentradaini;
              $this->mdEntrada->updated_at     = $dataentradaini;
              $this->mdEntrada->tipo           = $request->input('tipo');
              $this->mdEntrada->valor          = $request->input('valor');
              $this->mdEntrada->area           = $request->input('area');
              $this->mdEntrada->observacao     = $request->input('observacao');
              $this->mdEntrada->user_id        = $request->input('user_id');                 
              $this->mdEntrada->flag           = "U";                  
              $this->mdEntrada->save();


       }    

        $response["success"]             = 1;
        $response["message"]             = "Entrada efetuada com sucesso.TIPO:1";
        $response["entradaid"]           = $this->mdEntrada->id;

        return $response;       

}

我猜想是用户按了太多次发送按钮,但是我花了一点时间启用和不启用按钮,因为它们不会同时发出很多请求,这是代码按钮:

 sendButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {


            YoYo.with(Techniques.BounceIn)
                    .duration(500)
                    .playOn(v.findViewById(R.id.entradabt));


            if (SystemClock.elapsedRealtime() - mLastClickTime < 3000){
                return;
            }
            mLastClickTime = SystemClock.elapsedRealtime();
            enviarEntrada();

        }
    });

为什么我会有这种重复的记录?有提示吗?

here is the new image from database

这是我的架构

my schema

1 个答案:

答案 0 :(得分:0)

Unique_Key

在Created_at,Updated_at和Placa上添加唯一键,因为这可能是该表的唯一标识符。这样可以防止同时插入。

检查,如果一个请求多次发送SAME请求,并通过PLACA(拼版?)过滤掉(单个请求中只能发送1个普拉卡)。

可以方便吗? 如果同一PLACA在支付相同请求后1分钟内发送邮件,请忽略该请求。 (SQL查询过滤器,在最后一分钟内请求所有事务,并检查是否存在(carplate)广场)。 result = SQL Query select all where placa is within 1 and or 2 min; if(result is same PLACA as request){ return; and ignore request)

需要帮助的伪代码。