Insert new records and edit existing SQL

0

I am uploading an excel through a form, this excel contains product records which must be inserted in the database to keep track of inventory in the system.

I have the problem that I need to upload the excel system to insert the new products I get in the excel and edit those that already exist.

For this I declare the code field of the products table as UNIQUE field to be able to do what I mention, when using INSERT INTO products ON DUPLICATE KEY UPDATE as I show in my code, the work of updating existing and inserting new ones does it well, the problem is that at the moment of inserting the new records the last autoincrementable id of the table is skipped according to the number of records that exist in the table. For example:

So is my table with the existing products:

**id - codigo - producto - precio**
   1 -  CCC1  - samsung  - 150
   2 -  CCC2  - LG       - 120
   3 -  CCC3  - Huawei   - 150

When performing the INSERT INTO products ON DUPLICATE KEY UPDATE and there are new records in the excel, the table is as follows:

**id - codigo - producto - precio**
   1 -  CCC1  - samsung  - 150
   2 -  CCC2  - LG       - 120
   3 -  CCC3  - Huawei   - 180
   7 -  CCC4  - Nokia    - 90

The function of updating the existing ones well and inserting new ones, but as you can see the id autoincremento instead of inserting the number 4, I insert the 7 and this generates problems for me, besides that it is not right that leave like that.

What I would like to know is how I can do the update and insertion process without using the INSERT INTO ON KEY UPDATE.

$sql = $this->db->prepare("SELECT codigo FROM productos");
$sql->execute();
$array = $sql->fetchAll();

for($i = 13; $i <= $numRows; $i++){
    $cod = $objPHPExcel->getActiveSheet()->getCell('A'.$i)->getCalculatedValue();
    $descri = $objPHPExcel->getActiveSheet()->getCell('B'.$i)->getCalculatedValue();
    $uxc = $objPHPExcel->getActiveSheet()->getCell('C'.$i)->getCalculatedValue();
    $precioA = $objPHPExcel->getActiveSheet()->getCell('D'.$i)->getCalculatedValue();
                    $precioC = $objPHPExcel->getActiveSheet()->getCell('E'.$i)->getCalculatedValue();
    $existencia = $objPHPExcel->getActiveSheet()->getCell('F'.$i)->getCalculatedValue();
    $und = $objPHPExcel->getActiveSheet()->getCell('G'.$i)->getCalculatedValue();



   if (in_array($cod,$array)) {
    $sql = $this->db->prepare("UPDATE productos SET descripcion = ?, precioA = ?, precioC = ?, uxc = ? WHERE codigo = ?");
                        $sql->execute(array($descri,$precioA,$precioC,$uxc,$cod));
                        $json['error'] = false;
   }else{
    $sql = $this->db->prepare("INSERT INTO productos(codigo,descripcion,precioA,precioC,uxc,visible)VALUES(?,?,?,?,?,1)");
        $sql->execute(array($cod,$descri,$precioA,$precioC,$uxc));
        $json['error'] = false;
    }
}
    
asked by Alejo Mendoza 10.09.2018 в 21:59
source

1 answer

0

First you should check with:

$sql = $this->db->prepare("select count(*) as conteo from productos where codigo = ?");

And if counting is greater than zero, update is used, otherwise insert is used, that avoids an incorrect use of insert .

    
answered by 11.09.2018 в 17:26