Error with AUTO_INCREMENT ID

Asked

Viewed 130 times

0

My problem is to leave the first INSERT field empty,(if I fill the program runs), in my phpmyadmin the ID field is AUTO_INCREMENT, but without modifying that part of the code, it stopped.

Code:

$sql = "INSERT INTO usuarios VALUES ('','$user','$sobrenome','$sexo','$nascimento','$senha','$email','$foto','$status')";

$busca = mysqli_query($link,$sql);

$afetado = mysqli_affected_rows($link);

2 answers

3


There are two ways to do INSERT, the way you do it, and tell what columns you want to insert

INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)  
VALUES (value1, value2, value3,...valueN);

More hints: https://www.tutorialspoint.com/sql/sql-insert-query.htm

I can’t mount SQL for you because I don’t know the columns, but it’s easy ;)

  • $search = mysqli_query($link,$sql);

  • Here is the error, is returning false.

  • @Gabrielcordeiro you changed the code to be the columns you want to enter and the corresponding values? I did not understand your comment.

  • 1

    ignore my good kkk comment I did what you recommended and also needed to add "null" in the ID value. OBG

  • I have no way to test now, but try to insert "skipping" the ID column in both the column list and VALUES, I think it should work. But I’m glad he helped you ;)

3

When you are entering data into a table that has an AUTO_INCREMENT ID, without specifying the columns you will be filling (which is the case with your example), you should leave the ID value with 'null'.

$sql = "INSERT INTO usuarios VALUES (null,'$user','$sobrenome','$sexo','$nascimento','$senha','$email','$foto','$status')";

Another way is to declare the fields where the data will be entered in the table. Informing the table fields before the VALUES within parentheses.

$sql = "INSERT INTO usuarios (user, senha, email) VALUES ('$user','$senha','$email')";
  • That was it... Obg

  • You’re welcome! Tmj ;)

Browser other questions tagged

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