Problem with LAST_INSERT_ID

0

I am trying to get the next number in a column called TARIMA , the problem is that% is not% co_ since it is apart from autoincremento general ID , try to use AI to get the next lastinsertid of the table but it does not return anything.

$rs = mysqli_query($con,"SELECT MAX(tarima) FROM tarima_detalles");
        if ($row = mysqli_fetch_row($rs)) {
        $codigo = trim($row[0]);

         if(mysqli_query($con,"INSERT INTO tarima_detalles (TARIMA, SEPARACION, Cajas, Peso,calidad) VALUES ('$codigo','$separacion','$cajas','$peso','$calidad')")){
              echo(mysqli_insert_id($con));
          }else{
               echo("Error description: " . mysqli_error($con));
          }
          mysqli_close($con);
        }else{
            echo "ERROR";

It only repeats data to me, except if it was empty it does not insert anything .:

    
asked by DoubleM 23.02.2018 в 10:13
source

2 answers

1

The problem is that the function Max() returns the highest result of a list, that is

Having the numbers: 2, 1, 6, 7

When using Max(): 7

In your case when you make the Max -> MAX(tarima) your highest number in the list is the 1 , therefore, the result of Max() is 1 .

You can use MAX(tarima) but before inserting, you add + 1 .

    
answered by 23.02.2018 в 10:33
0

You can get it directly with this query:

SELECT tarima + 1 AS tarima_nueva
FROM tarima_detalles 
ORDER BY id DESC 
LIMIT 1

What it does is search for the record with the highest id (which would always be the most recent since it was autoincremental), and add 1 to its value tarima . Then, the value would be obtained in $row['tarima_nueva'] .

    
answered by 27.02.2018 в 00:38