How to do insertion at the same time without giving duplicate key error?

-4
<section class="content">
    <div class="row">
      <div class="col-md-12">
        <div class="box box-primary">
          <!-- form start -->
          <form role="form" class="form-horizontal" action="modules/medicines_transaction/proses.php?act=insert" method="POST" name="formObatMasuk">
            <div class="box-body">
              <?php  

              $query_id = mysqli_query($mysqli, "SELECT RIGHT(codigo_transaccion,7) as codigo FROM transaccion_medicamentos
                                                ORDER BY codigo_transaccion DESC LIMIT 1")
                                                or die('Error : '.mysqli_error($mysqli));

              $count = mysqli_num_rows($query_id);

//mysql_query('INSERT INTO ...');
//if (mysql_errno() == 1062) {
   // print 'no way!';}

              if ($count <> 0) {

                  $data_id = mysqli_fetch_assoc($query_id);
                  $codigo    = $data_id['codigo']+1;
              } else {
                  $codigo = 1;
              }


              $tahun          = date("Y");
              $buat_id        = str_pad($codigo, 7, "0", STR_PAD_LEFT);
              $codigo_transaccion = "ES-$tahun-$buat_id";
              ?>

              <div class="form-group">
                <label class="col-sm-2 control-label">Codigo da Operação </label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" name="codigo_transaccion" value="<?php echo $codigo_transaccion; ?>" readonly required>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label">Data</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" data-date-format="dd-mm-yyyy" name="fecha_a" autocomplete="off" value="<?php echo date("d-m-Y"); ?>" readonly required>
                </div>
              </div>


               <div class="form-group">
                <label class="col-sm-2 control-label">Hora</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control"  name="created_date" autocomplete="off" value="<?php echo date("H:i"); ?>" readonly required>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label">Data de Expiração</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control date-picker" data-date-format="dd-mm-yyyy"  name="expdata" autocomplete="off" >
                </div>
              </div>


             <div class="form-group">
                <label class="col-sm-2 control-label">Fornecedor</label>
                <div class="col-sm-5">
                  <select class="chosen-select" name="origem_destino" data-placeholder="Selecione um Fornecedor"  autocomplete="off" >
                    <option value=""></option>
                    <option value="Dep.C.Filhos">Dep.C.Filhos</option>
                    <option value="Farwell">Farwell</option>
                    <option value="ElnorFarma">ElnorFarma</option>
                    <option value="O.Kussonga">O.Kussonga</option>
                    <option value="UnicaFarma">UnicaFarma</option>
                    <option value="NCR">NCR</option>
                    <option value="D.P.S.M.L">D.P.S.M.L</option>

                  </select>
                </div>
              </div>



              <hr>

              <div class="form-group">  
                <label class="col-sm-2 control-label">Produto</label>
                <div class="col-sm-5">
                  <select class="chosen-select" name="codigo" data-placeholder="  Seleccione um Producto " onchange="tampil_obat(this)&limpa()" autocomplete="off" required>
                    <option value=""></option>
                    <?php
                      $query_obat = mysqli_query($mysqli, "SELECT codigo, produto FROM Medicamentos ORDER BY produto ASC")
                                                            or die('error '.mysqli_error($mysqli));
                      while ($data_obat = mysqli_fetch_assoc($query_obat)) {
                        echo"<option value=\"$data_obat[codigo]\"> $data_obat[codigo] | $data_obat[produto] </option>";
                      }
                    ?>
                  </select>
                </div>
              </div>

              <span id='stok'>
              <div class="form-group">
                <label class="col-sm-2 control-label">Stock</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" id="stok" name="stock" readonly required>
                </div>
              </div>
              </span>

              <div class="form-group">
                <label class="col-sm-2 control-label">Quantidade</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" id="jumlah_masuk" placeholder="0" name="num" autocomplete="off" onKeyPress="return goodchars(event,'0123456789',this)" onkeyup="hitung_total_stok(this)&cek_jumlah_masuk(this)" maxlength="5" required>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label">Operação</label>
                <div class="col-sm-5">
                  <select name="transaccion" id="transaccion"  required class='form-control' onchange="hitung_total_stok();">
                    <option value="Entrada">Entrada</option>
                    <option value="Saida">Saida</option>
                  </select>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label">Total Stock</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" id="total_stok" name="total_stock" readonly required>
                </div>
              </div>

            </div><!-- /.box body -->

            <div class="box-footer">
              <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                  <input type="submit" class="btn btn-primary btn-submit" name="Guardar" value="Guardar">
                  <a href="?module=medicines_transaction" class="btn btn-default btn-reset">Cancelar</a>
                </div>
              </div>
    
asked by anonymous 28.12.2018 / 22:10

1 answer

1

I'll ignore the poor quality of the question body and focus on the title:

  

- How to do insertion at the same time without giving duplicate key error?

And in the comment:

  

Duplicate key should not be inserted;

Query with the IGNORE option:

INSERT IGNORE INTO 'tabela' VALUES ();

Remembering that it will ignore duplicate key errors and will only generate a warning.

See this answer that explains how it works (free translation):

  

If you use INSERT IGNORE , the line will not be inserted if it results in a duplicate key. But the statement will not generate an error. Instead, it generates a warning. These cases include:

     
  • Inserting a duplicate key into columns with PRIMARY KEY or UNIQUE constraints.

  •   
  • Inserting a NULL into a column with a NOT NULL constraint.

  •   
  • By inserting a row in a partitioned table, but the values entered are not mapped to a partition.

  •   

SOen - "INSERT IGNORE" vs. "INSERT ... ON DUPLICATE KEY UPDATE"

    
28.12.2018 / 23:08