How to insert the ID in the database since it is the first field of the table?

Asked

Viewed 148 times

1

I have auto increment ID and am having error counting columns with this code. How can I enter the ID together?

    #SQL Execute
    $uir = $pdo->prepare("INSERT INTO visitados VALUES (:IMO_CODIGO, :CATEGORIA, :BAIRRO, :FOTO_PRINCIPAL, :VLR_VENDA, :AREA_TOTAL, :DORMITORIO, :DATA)");

    $uir->bindParam(':IMO_CODIGO', $imov);
    $uir->bindParam(':CATEGORIA', $cate);
    $uir->bindParam(':BAIRRO', $bair);
    $uir->bindParam(':FOTO_PRINCIPAL', $foto);
    $uir->bindParam(':VLR_VENDA', $vlrv);
    $uir->bindParam(':AREA_TOTAL', $area);
    $uir->bindParam(':DORMITORIO', $dorm);
    $uir->bindParam(':DATA', $adata);

    $uir->execute();

2 answers

3


You need to explicitly state the name of the columns you will be doing INSERT if you want to omit a field, e.g.:

INSERT INTO visitados (nomecoluna, outronomecoluna, maisumnomecoluna ...) VALUES (:IMO_CODIGO, :CATEGORIA, :BAIRRO ...)
  • Let the bank handle the id od and the rest you can insert in the way the friend proposes.

3

No doubt it is better to declare the names of the fields according to the reply of wryel, even to not need to obey the exact order that is in the table.

But for the sake of information, it would also work this way:

#SQL Execute
    $uir = $pdo->prepare("INSERT INTO visitados VALUES (NULL, :IMO_CODIGO, :CATEGORIA, :BAIRRO, :FOTO_PRINCIPAL, :VLR_VENDA, :AREA_TOTAL, :DORMITORIO, :DATA)");

Browser other questions tagged

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