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

Asked

Viewed 69 times

-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>
  • Sets the column as autoincrement and passes null in the insert

  • When you say: - without giving duplicate key error means that: 1) duplicate key must be inserted anyway; 2) duplicate key must not be inserted; 3) duplicate key must be renamed and entered; or 4) ignore duplicate keys ?

    1. duplicate key should not be inserted;
  • William main column is varchar, does not accept auto_increment

1 answer

1


I will ignore the low quality of the body of the question and focus on the title:

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

And in the commentary:

- duplicate key should not be inserted;

Make your query with the option IGNORE:

INSERT IGNORE INTO `tabela` VALUES ();

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

See this answer that explains well 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 constrained columns PRIMARY KEY or UNIQUE.

  • Inserting a NULL in a column with a restriction NOT NULL.

  • Inserting a row in a Partified table, but inserted values are not mapped to a partition.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.