Error with ID AUTO_INCREMENT [closed]

0

My problem is to leave the first INSERT field empty, (if I fill in the program), 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);
    
asked by anonymous 20.12.2016 / 01:30

2 answers

3

There are two ways to do INSERT, the way you are doing, and which columns you want to insert

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

More infos: link

I can not mount SQL for you because I do not know the columns, but it's easy;)

    
20.12.2016 / 01:55
3

As long as you are inserting data into a table that has an ID with AUTO_INCREMENT, without specifying the columns it will be populating (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 inserted into the table. Informing the table fields before VALUES within parentheses.

$sql = "INSERT INTO usuarios (user, senha, email) VALUES ('$user','$senha','$email')";
    
20.12.2016 / 02:22