Run ID in sequence without auto increment

Asked

Viewed 63 times

0

I have a bank problem. When registering a user the ID is set to 0. I try to use Auto Increment, but when I activate this option, the other entries stop working.

  • 1

    No code, no help.

  • Which you can’t? In phpmyadmin or in php code?

  • It is common in these cases to create a function that retrieves the last ID and adds + 1

1 answer

0

If I understand correctly, you want to PHP, so you can do it this way.

First make a SELECT of all users before inserting, use a code similar to this

$este=mysqli_query($variavel_db, "SELECT * FROM tabela order by id DESC LIMIT 0, 1");

With the above code you will find the last inserted. If it does not exist, just a small condition that you insert with id = 1, that is, see the code summarized.

$este=mysqli_query($variavel_db, "SELECT * FROM tabela order by id DESC LIMIT 0, 1");
if(mysqli_num_rows($este) == 0){
   $insert=mysqli_query($variavel_db, "INSERT INTO tabela (id, valor1, valor2, etc) VALUES (1, valor1, valor2, etc)");
}else{
   $mos=mysqli_fetch_assoc($este);
   $plus=$mos['id']+1;
   $insert=mysqli_query($variavel_db, "INSERT INTO tabela (id, valor1, valor2, etc) VALUES ('$plus', 'valor1', 'valor2', etc)");
}

Browser other questions tagged

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